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... -- Posted via http://www.ruby-forum.com/.
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 :> 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... > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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... > endThat 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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
Roman LE NEGRATE
2006-May-31 18:18 UTC
[Rails] Re: habtm on an array (or object.habtm.habtm)
Le 31 mai 06 ? 18:56, Josh Susser 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 ?
Josh Susser
2006-May-31 21:36 UTC
[Rails] Re: Re: habtm on an array (or object.habtm.habtm)
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 itIt''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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
Roman LE NEGRATE
2006-Jun-01 17:34 UTC
[Rails] Re: Re: habtm on an array (or object.habtm.habtm)
> 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- > shorthandGood ! Thanks.
Roman LE NEGRATE
2006-Jun-10 21:22 UTC
[Rails] Re: Re: habtm on an array (or object.habtm.habtm)
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