[Rails] Re: Re: Re: Modelling Foreign Keys
Chris Hall
christopher.k.hall at gmail.com
Mon Apr 3 16:57:38 GMT 2006
associations are a two way street.
in the student advisors example
# student belongs_to :advisor
s = Student.find(1, :include => :advisor) # find student with id = 1
puts s.advisor.name # print name of the student's advisor
# advisor has_many :students
a = Advisor.find(1, :include => :students) # find advisor with id = 1
a.students.each { |s| puts s.name } # print all the names of the students
for this advisor
On 3/31/06, Arch Stanton <d6veteran at gmail.com> wrote:
>
> Chris Hall wrote:
> > you're not getting anything. Rails figures out that info based on the
> > criteria you specify. if you stick to Rails conventions, you don't have
> > to
> > do anything but provide the association name, rails does the rest.
> >
> > 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/.
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060403/c24729c7/attachment.html
More information about the Rails
mailing list