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.
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 [email protected] http://lists.rubyonrails.org/mailman/listinfo/rails
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:
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:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.