I am using Ruby on Rails 3.0.10 and I would like to improve some code (keep reading for more information) that retrieves data from the database using an :has_many :through
association.
In the model I have:
class Article < ActiveRecord::Base
has_many :category_relationships
has_many :categories,
:through => :category_relationships,
end
I would like to improve the following code (that retrieves article categories objects "filtering" some of those by using a where
statement) so to follow the "Ruby on Rails Way of doing things":
@articles.category_relationships.where(:comment_id => @comment.id).map{ |category_relationship| category_relationship.article_category }
How can\should I do that? Can\Should I "work" on the @articles.categories
association in order to improve the above code? If so, how?