class Result < ActiveRecord::Base
has_many :collections
has_many :articles, :through => :collections
end
class Article < ActiveRecord::Base
has_many :collections
has_many :results, :through => :collections
end
class Collection < ActiveRecord::Base
belongs_to :result
belongs_to :article
end
Collection has a table like this:
create_table :collections do |t|
t.column :id, :integer
t.column :result_id, :integer
t.column :article_id, :integer
t.column :extract, :text
end
The problem i have is this.
I do a find for a single result.
Then i go through each article in this result, using the each method.
At this point i'm outputting all the attributes of articles.
But i want to also output the extract for this article and result.
How would i do this?
article.extract doesn't work, nor does article.results.extract
In your design article has many collections therefor article also has
many extracts. Which means that article will not have an extract
method which is why article.extract will not work.
In the same way article.results method will give you an Array of
Result object so obviously will not have an extract method. This is
why articles.results.extract will not work.
Maybe that will help you rethink your design to produce your desired
results.
use eager loading. That will only take one SQL query, and you will
have all the stuff you need in your result.
(code not tested and probably contains typos )