Mike Harris
10/31/2006 8:04:00 PM
Ike wrote:
>In my model, if I have a field, that contains the id to another table, twice
>(like, primary associate and a secondary associate) I am specifying this as
>
>class Customer < ActiveRecord::Base
> belongs_to: associate :foreign_key => "associatekey1"
> belongs_to: associate :foreign_key => "associatekey2"
>end
>
>This doesn't work -- but my question is how SHOULD the syntax for specifying
>this look? Thanks, Ike
>
>
>
>
>
>
You probably could have looked it up, and it should go on the rails
list. However, people don't need to be rude. RTFM should be banned
from this list, plus the rails documentation is scattered and it sorta
sucks.
Using the column names primary_associate_id and secondary_associate_id,
instead of associatekey1 and associatekey2
class Customer < ActiveRecord::Base
belongs_to :primary_associate, :class_name => 'Associate',
:foreign_key => 'primary_associate_id'
belongs_to :secondary_associate, :class_name => 'Associate',
:foreign_key => 'secondary_associate_id'
end
Rails automatically infers the class_name and foreign_key from the name
of your association. You can always just name your association whatever
you want and specify those things explicitly, like I do here.