[Rails] Re: Re: Modelling Foreign Keys

Chris Hall christopher.k.hall at gmail.com
Sat Apr 1 02:52:35 GMT 2006


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"

example 2:

student belongs_to :advisor, :class_name => "Person"

in this case, we must tell the model to not make the default assumptions,
since our association name has nothing to do with what we really want.  you
have to be explicit if you don't want to stick to Rails conventions


On 3/31/06, Arch Stanton <d6veteran at gmail.com> wrote:
>
> Chris Hall wrote:
> > models:
> >
> > class User < ActiveRecord::Base
> >   has_many :posts
> > end
> >
> > class Post < ActiveRecord::Base
> >   beongs_to :user #
> > end
> >
>
>
> Thanks Chris.
>
> So, what do I get from explicitly identifying the foreign key
> relationship in the model (like belongs_to :advisor,
> :class_name=>"Advisor", :foreign_key =>
> "advisor_id")??
>
> Thanks.
>
> --
> 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/20060401/e20075ee/attachment.html


More information about the Rails mailing list