Forum: Ruby on Rails Easier way to pull associated records of has_many associated

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ko N. (Guest)
on 2008-10-03 06:20
Currently my User model has has_many groups and the Group model has_many

so is there any way to pulling all the folders like this.

Currently I have to loop like this and dump them in to array.

[code=]  def user_folders
    user_folders = []
    self.groups.each do |group|
    user_folders = user_folders.flatten.uniq

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

Group model
[code=]  has_many :members
  has_many :users, :through => :members
  has_many :permissions
  has_many :folders, :through => :permissions

So is there a easy way to pull user.groups.folders without looping
through each group and saving folder in a array?
brady8 (Guest)
on 2008-10-03 11:06
(Received via mailing list)
If you want all folders belonging to all groups for a user, you can

user.groups.collect{|g| g.folders}.flatten.uniq

which will return all unique folders in an array.

This topic is locked and can not be replied to.