An entry can only belong to a category right? If this is the case you don't
need this many-to-many association. You just have to:

class Category < ActiveRecord::Base
 has_many :entries
end

class Entry < ActiveRecord::Base
 belongs_to :user
 belongs_to :category
end

This will let you do:

my_entry.category.name
my_category.entry

If you want to keep working with that model then you have an error in your
query. Using :include in finders will modify the structure of your SQL
query. So you have to refer to table names in conditions like this:

:conditions => ['users.user_id = 1']

On Tue, Sep 23, 2008 at 2:00 PM, ressister <[EMAIL PROTECTED]> wrote:

>
> 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_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
> one category.
>
> Here are the models:
>
> class EntryCategory < ActiveRecord::Base
>  belongs_to :entry
>  belongs_to :category
> end
>
> class Category < ActiveRecord::Base
>  has_many :entry_categories
>  has_many :entries, :through => :entry_categories
> end
>
> class Entry < ActiveRecord::Base
>  belongs_to :user
>  has_one :entry_category
>  has_one :category, :through => :entry_category
> end
>
> 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.
>
> Thanks,
> -Alex
> >
>


-- 
ANDRES RODRIGUEZ E
Electronic Engineer
Software Developer
IT Consultant

US Phone: (305) 853-8356
Primary Mobile: +57-300-2006186
Secondary Mobile: +57-314-7939859

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to