If I have a class, Asset, which has and belongs to many associated Asset objects, how do I do this? has_and_belongs_to_many :associated_assets, :class => "Asset", :foreign_key => ?????, :association_foreign_key => ????? Right now, my table has a :first_asset_id and a :second_asset_id, but I don''t see how the objects themselves are supposed to distinguish one from the other. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Surely someone here has done this? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nevermind, I guess a join model is the only way to go. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dave Smith wrote:> If I have a class, Asset, which has and belongs to many associated Asset > objects, how do I do this? > > has_and_belongs_to_many :associated_assets, :class => "Asset", > :foreign_key => ?????, :association_foreign_key => ????? > > Right now, my table has a :first_asset_id and a :second_asset_id, but I > don''t see how the objects themselves are supposed to distinguish one > from the other.You should be able to do this by using TWO associations and specifying a slew of options. You need to override pretty much all the basic defaults. Create a join table called ''assets_assets'' for the below example. Asset: has_and_belongs_to_many :first_assets, :class_name => "Asset", :join_table => "assets_assets", :foreign_key => "second_asset_id", :association_foreign_key => "first_asset_id" has_and_belongs_to_many :second_assets, :class_name => "Asset", :join_table => "assets_assets", :foreign_key => "first_asset_id", :association_foreign_key => "second_asset_id" Change those association names as you want. Note, this is written in the email so it''s untested. It''s straightforward though so I''d expect it to work. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dave, with a web site with that name I am sure Josh knows a lot more about this than I do, but in the case you describe it looks like you just want a simple self-join. In which case I think what you want is: def Asset < ActiveRecord::Base has_and_belongs_to_many :related_assets, :class_name => "Article", :join_table => "related_assets", :association_foreign_key => "related_asset_id", :foreign_key => "asset_id" end And then a table called related_assets with the columns related_asset_id and asset_id Cheers, --Kip On Sep 7, 6:43 am, Josh Susser <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Dave Smith wrote: > > If I have a class, Asset, which has and belongs to many associated Asset > > objects, how do I do this? > > > has_and_belongs_to_many :associated_assets, :class => "Asset", > > :foreign_key => ?????, :association_foreign_key => ????? > > > Right now, my table has a :first_asset_id and a :second_asset_id, but I > > don''t see how the objects themselves are supposed to distinguish one > > from the other. > > You should be able to do this by using TWO associations and specifying a > slew of options. You need to override pretty much all the basic > defaults. Create a join table called ''assets_assets'' for the below > example. > > Asset: > has_and_belongs_to_many :first_assets, :class_name => "Asset", > :join_table => "assets_assets", :foreign_key => "second_asset_id", > :association_foreign_key => "first_asset_id" > has_and_belongs_to_many :second_assets, :class_name => "Asset", > :join_table => "assets_assets", :foreign_key => "first_asset_id", > :association_foreign_key => "second_asset_id" > > Change those association names as you want. Note, this is written in the > email so it''s untested. It''s straightforward though so I''d expect it to > work. > > -- > Josh Susserhttp://blog.hasmanythrough.com > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---