[Rails] Re: Re: Re: Modelling Foreign Keys

Steve Koppelman hatlessnyc at yahoo.com
Mon Apr 3 16:40:54 GMT 2006


The point of telling Rails about the relationships between models is not 
because Rails will magically create scaffolding for you but because 
you'll be able to access you data through the relations.

For instance, once you've set up those two relationships in your models, 
you can do things like

@advisor = Advisor.find(1)
@students = @advisor.students

and conversely, if you have a student selected into @student, you can 
get the advisor's name simply via @student.advisor.name

Or if you're updating a student's attributes in a controller action, if 
you set some of @student.advisor's attributes, the changes to the 
advisor will also get saved when you simply call @student.save

and all sorts of stuff like that.

Arch Stanton wrote:
> Chris Hall wrote:
.... snip ...
>> so in that example
>> 
>> student belongs_to :advisor
>> 
>> rails will make the following assumptions:
>> 
>> students table contains an "advisor_id" column that references the id 
>> column
>> in advisors table
>> the associated class is named "Advisor"
>> 
> 
> 
> I guess I do have a follow up question.
> 
> In the above example, what is gained by adding a has_many relationship 
> to Advisor?
> 
> advisor has_many :students


-- 
Posted via http://www.ruby-forum.com/.


More information about the Rails mailing list