I'm having trouble with three models that need to play nice together:
- entry <- stores diary entries
- entry_category <- links a diary entry to a category (entry_id,
- category <- list of category names and attributes
I want to be able to :include => :categories when I perform a find,
but I'm not sure if I have the model associations wrong or if my find
syntax is wrong or both? An entry should only be able to be a part of
Here are the models:
class EntryCategory < ActiveRecord::Base
class Category < ActiveRecord::Base
has_many :entries, :through => :entry_categories
class Entry < ActiveRecord::Base
has_one :category, :through => :entry_category
I want to sum the price of all entries and include the category name.
I can get all the entries grouped properly like so....
@entries = Entry.sum(:price, :conditions => ['user_id = 1'], :include
=> :entry_category, :group => 'entry_categories.category_id')
...how do I also get the category.name? If I try and include
categories, the query doesn't work which makes me think i have a
problem with my model associations.
Any help would be greatly appreciated.