I have a Users model. Users 'have many' Stories, and Stories have
many Items.
This structure allows the sharing of Items between Stories and Stories
between Users.
But given there is no direct 'ownership' link between Users and Items,
what is the best way of building a list of Items which belong to a
Story to which a given User has access.
something like:
current_user.stories.items
obviously doesn't work.
I've written some nested blocks ie:
users.each do |user|
user.stories.each.do |story|
story.items.each.do |item|
end
end
end
which works but is ugly and slow.
I could keep an index table of items linked to users but this is
redundant for most of my application. I'm sure there is a more
obvious, simple way of doing this but I haven't come across it.
Yes that makes sense, and I wasn't aware it was possible. However
having added to my User model:
has_many :items, :through=>:stories
I am getting the following error when trying to call @user.items
ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid
source reflection macro :has_many :through for
has_many :items, :through => :stories. Use :source to specify the
source reflection.
I have to admit Reflections are something beyond my knowledge, and I
noted suggestions that nested HABTM associations weren't supported in
earlier versions of Rails.