Forum: Ruby on Rails habtm on an array (or object.habtm.habtm)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel (Guest)
on 2006-05-31 16:27
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...
Roman LE NEGRATE (Guest)
on 2006-05-31 17:07
(Received via mailing list)
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 :
Josh S. (Guest)
on 2006-05-31 20:56
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
Roman LE NEGRATE (Guest)
on 2006-05-31 22:22
(Received via mailing list)
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
Josh S. (Guest)
on 2006-06-01 01:36
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...

--
Josh S.
http://blog.hasmanythrough.com
Roman LE NEGRATE (Guest)
on 2006-06-01 21:35
(Received via mailing list)
> 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...
> shorthand

Good ! Thanks.
Roman LE NEGRATE (Guest)
on 2006-06-11 01:23
(Received via mailing list)
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...
shorthand#comment-324

Kind regards,
Roman
This topic is locked and can not be replied to.