Preethi Sivakumar
2009-Feb-28 05:54 UTC
has_many :through, expects "through" model to be belongs_to
Hi, I''m using has_many and through relationship between two models Profile and User Profile.rb ----------- has_and_belongs_to_many :users belongs_to :community User.rb ------- has_and_belongs_to_many :profiles has_many :communities, :through=>profiles When i try to do @user.communities, Rails is searching for foreign key of user in profile table, whereas profile and user have habtm relationship. How to solve this problem? -- 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 -~----------~----~----~----~------~----~------~--~---
Sijo Kg
2009-Feb-28 07:16 UTC
Re: has_many :through, expects "through" model to be belongs
suppose if the junction model is ProfileUser You can define like Profile.rb> -----------has_many :profile_users has_many :users, :through => :profile_users>User.rb> -------has_many :profile_users has_many :profiles, :through => :profile_users profile_users.rb belongs_to :user belongs_to :profile Now you can access like @user.profiles Is that what u want? Sijo -- 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 -~----------~----~----~----~------~----~------~--~---
Julian Leviston
2009-Feb-28 07:56 UTC
Re: has_many :through, expects "through" model to be belongs_to
You can only go through has_many associations. Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/ On 28/02/2009, at 4:54 PM, Preethi Sivakumar <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > wrote:> > Hi, > I''m using has_many and through relationship between two models > Profile and User > Profile.rb > ----------- > has_and_belongs_to_many :users > belongs_to :community > > User.rb > ------- > has_and_belongs_to_many :profiles > has_many :communities, :through=>profiles > > When i try to do > @user.communities, Rails is searching for foreign key of user in > profile > table, whereas profile and user have habtm relationship. > How to solve this problem? > -- > 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 -~----------~----~----~----~------~----~------~--~---
Neetal Sharma
2009-Feb-28 10:38 UTC
Re: has_many :through, expects "through" model to be belongs
Sijo Kg wrote:> suppose if the junction model is ProfileUser You can define like > > Profile.rb >> ----------- > has_many :profile_users > has_many :users, :through => :profile_users >> > User.rb >> ------- > has_many :profile_users > has_many :profiles, :through => :profile_users > > profile_users.rb > belongs_to :user > belongs_to :profile > > Now you can access like @user.profiles Is that what u want? > > SijoThanks for your input. I''m able to do @user.profiles, that is not creating any problem. The problem is, User: -------- has_many :profile_users has_many :profiles, through=>:profile_users has_many :community, through=>:profiles where community --------- has_many :profiles has_many :users , through=>:profiles and profile ------- belongs_to :community profiles_users -------------- belongs_to :profile belongs_to :user When do @user.communities, flow should be like this: ----------------------------------------------------------------------------- Join the profile and profile user table and get all the profiles that belong to the user and, from the profiles check the community it belongs to and then return the result(communities). but am not getting the desired result because rails is not joining all the tables needed, it is checking for community_id in profile_users whereas it is in profile table. -- 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 -~----------~----~----~----~------~----~------~--~---