Habtm on an array (or object.habtm.habtm)


#1

Hi,

I have three Models: User, Group and File.

User habtm groups
and
Group habtm Files

How can I find all files that a particular user has access to?
eg user.groups.files ?

As user.groups returns an array, the above doesn’t work.

Id like to find the files for all groups in that array, without adding
any code to the User model.

Thanks in advance

PS I just posted this in the ‘Ruby’ Forum by accident, so sorry about
the double post…


#2

Hi Daniel,
I think you must create a method User#files which should look like
this :

class User < ActiveRecord::Base
# …rest…
def groups_files
groups.map {|g| g.files }.flatten
end
# …rest…
end

Hope it helps :-).

Le 31 mai 06 à 14:27, Daniel a écrit :


#3

Roman LE NEGRATE wrote:

Hi Daniel,
I think you must create a method User#files which should look like
this :

class User < ActiveRecord::Base

…rest…

def groups_files
groups.map {|g| g.files }.flatten
end

…rest…

end

That will work, but when you do that, you probably want an :include =>
:files on the groups finder so that you don’t have a separate database
query for every group the user has.

def groups_files
	groups.find(:all, :include => :files).map(&:files).flatten.uniq
end

The map(&:files) is just a shorter way of saying what Roman said with
the other syntax, and the uniq method removes duplicates.


Josh S.
http://blog.hasmanythrough.com


#4

Le 31 mai 06 à 18:56, Josh S. a écrit :

That will work, but when you do that, you probably want an :include =>
:files on the groups finder so that you don’t have a separate database
query for every group the user has.

def groups_files
groups.find(:all, :include => :files).map(&:files).flatten.uniq
end

The map(&:files) is just a shorter way of saying what Roman said with
the other syntax, and the uniq method removes duplicates.

Thank you Josh. I did not know the map(&:method_id) trick. Does it
come with Ruby > 1.8.2 ? I could not find its documentation in the
PickAxe2 book. Could you please link us to informations about it
?_______________________________________________
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


#5

It’s not standard Ruby yet, thought I think it’s on the way in a
future
release. But as of Rails 1.1, it’s part of ActiveSupport. I was as
surprised as anyone when I first discovered it:

http://blog.hasmanythrough.com/articles/2006/03/07/symbol-to-proc-
shorthand

Good ! Thanks.


#6

Roman LE NEGRATE wrote:

Thank you Josh. I did not know the map(&:method_id) trick. Does it
come with Ruby > 1.8.2 ? I could not find its documentation in the
PickAxe2 book. Could you please link us to informations about it

It’s not standard Ruby yet, thought I think it’s on the way in a future
release. But as of Rails 1.1, it’s part of ActiveSupport. I was as
surprised as anyone when I first discovered it:

http://blog.hasmanythrough.com/articles/2006/03/07/symbol-to-proc-shorthand


Josh S.
http://blog.hasmanythrough.com


#7

If someone is interested in the speeds difference between obj.map {|
o| o.method_name } and obj.map(&:method_name) , I commented
Josh’s article with a benchmark and its results. It’s readable right
at: http://blog.hasmanythrough.com/articles/2006/03/07/symbol-to-proc-
shorthand#comment-324

Kind regards,
Roman