Ko None
2008-Oct-03 02:20 UTC
Easier way to pull associated records of has_many associated
Currently my User model has has_many groups and the Group model has_many folders. so is there any way to pulling all the folders like this. user.groups.folders? Currently I have to loop like this and dump them in to array. [code=] def user_folders user_folders = [] user_folders.push(self.folders.collect) self.groups.each do |group| user_folders.push(group.folders.collect) end user_folders = user_folders.flatten.uniq end [/code] I get this error "ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_many :through for has_many :group_folders, :through => :groups. Use :source to specify the source reflection." User model [code=] has_many :permissions has_many :folders, :through => :permissions has_many :members has_many :groups, :through => :members has_many :group_folders, :through => :groups, :source => :folders [/code] Group model [code=] has_many :members has_many :users, :through => :members has_many :permissions has_many :folders, :through => :permissions [/code] So is there a easy way to pull user.groups.folders without looping through each group and saving folder in a array? -- 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 -~----------~----~----~----~------~----~------~--~---
brady8
2008-Oct-03 07:05 UTC
Re: Easier way to pull associated records of has_many associated
If you want all folders belonging to all groups for a user, you can do: user.groups.collect{|g| g.folders}.flatten.uniq which will return all unique folders in an array. -Brady brady-vPvWGLPDSrsV+D8aMU/kSg@public.gmane.org On Oct 2, 8:20 pm, Ko None <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Currently my User model has has_many groups and the Group model has_many > folders. > > so is there any way to pulling all the folders like this. > user.groups.folders? > > Currently I have to loop like this and dump them in to array. > > [code=] def user_folders > user_folders = [] > user_folders.push(self.folders.collect) > self.groups.each do |group| > user_folders.push(group.folders.collect) > end > user_folders = user_folders.flatten.uniq > end > [/code] > > I get this error > "ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source > reflection macro :has_many :through for has_many :group_folders, > :through => :groups. Use :source to specify the source reflection." > > User model > [code=] has_many :permissions > has_many :folders, :through => :permissions > has_many :members > has_many :groups, :through => :members > has_many :group_folders, :through => :groups, :source => :folders > [/code] > > Group model > [code=] has_many :members > has_many :users, :through => :members > has_many :permissions > has_many :folders, :through => :permissions > [/code] > > So is there a easy way to pull user.groups.folders without looping > through each group and saving folder in a array? > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---