Skip to content
theJohnnyBrown edited this page Sep 14, 2010 · 12 revisions

3/27:

created models, views, controllers for people and groups. controller/view is very basic, for example the people view generates a page with each person’s name and below the name shows all the groups associated with this person.

Used rake db:migrate to create the tables

To see it working I used the ruby console to add a few basic rows to each table. Here’s how to do it so rails automatically handles the join table:

johnny@johnny-laptop:~/Documents/rails/acm_site$ script/console Loading development environment (Rails 2.3.5)
>> me = Person.new
=> #<Person id: nil, name: nil, email: nil, created_at: nil, updated_at: nil>
>> me.name = “johnny”
=> “johnny”
>> me.save
=> true
>> u = Person.new
=> #<Person id: nil, name: nil, email: nil, created_at: nil, updated_at: nil>
>> u.name = “DeGrassi”
=> “DeGrassi”
>> me.save
=> true
>> u.save
=> true

#created two new Person objects and saved them to the people table

>> me.groups.create
=> #<Group id: 1, name: nil, created_at: “2010-05-27 07:23:13”, updated_at: “2010-05-27 07:23:13”>
>> me.groups.create
=> #<Group id: 2, name: nil, created_at: “2010-05-27 07:23:17”, updated_at: “2010-05-27 07:23:17”>
>> me.groups0.people
=> [#<Person id: 1, name: “johnny”, email: nil, created_at: “2010-05-27 07:22:23”, updated_at: “2010-05-27 07:22:23”>]
>> u.groups.create
=> #<Group id: 3, name: nil, created_at: “2010-05-27 07:23:45”, updated_at: “2010-05-27 07:23:45”>
>> u.groups
=> [#<Group id: 3, name: nil, created_at: “2010-05-27 07:23:45”, updated_at: “2010-05-27 07:23:45”>]
>> me.groups
=> [#<Group id: 1, name: nil, created_at: “2010-05-27 07:23:13”, updated_at: “2010-05-27 07:23:13”>, #<Group id: 2, name: nil, created_at: “2010-05-27 07:23:17”, updated_at: “2010-05-27 07:23:17”>]
>> u.groups0.people
=> [#<Person id: 2, name: “DeGrassi”, email: nil, created_at: “2010-05-27 07:23:04”, updated_at: “2010-05-27 07:23:04”>]

used each Person to create new groups, which are now linked to that person, as (Person).groups shows. Now make sure to do x.save for all the people and groups, and you should then see the results at http://localhost:3000/people or /groups. Here’s how to link two rows that are already created:

>> u = Person.all1
=> #<Person id: 2, name: “DeGrassi”, email: nil, created_at: “2010-05-27 07:23:04”, updated_at: “2010-05-27 07:23:04”>
>> grp = Group.all0
=> #<Group id: 1, name: “winners”, created_at: “2010-05-27 07:23:13”, updated_at: “2010-05-27 07:48:43”>
>> u.groups << grp
=> [#<Group id: 3, name: “catz”, created_at: “2010-05-27 07:23:45”, updated_at: “2010-05-27 07:58:31”>, #<Group id: 1, name: “winners”, created_at: "2010-05-

It’s kinda weird to learn Ruby and this framework at the same time. The last line there might as well be magic to me right now. I think it involves a hash of some sort, whatever that is.

So database relations are pretty cool is what I learned.

also I slapped on some CSS that came with the tutorial.

Clone this wiki locally