I have a category model and several other models that I want to
categorize.
Categories are organised in a hierarchy.
class Category < ActiveRecord::Base
acts_as_tree
end
class User < ActiveRecord::Base
end
class Image < ActiveRecord::Base
end
I am quite sure that I am going to have additional models (like User
and Images) that I want to categorize later on.
It should be possible to assign a category to more than one object.
For example I have a category "new" and I want to assign this category
to several user and image records.
So many Images and Users could have many categories and vice versa.
I thought maybe it is possible to implement this with a
has_many :through association in combination with polymorphic => true,
but until now I could not fiqure out how to declare this in the models
and how my migrations have to look like...
c.categorizables
ActiveRecord::HasManyThroughAssociationPolymorphicError: Cannot have a
has_many :through association 'Category#categorizables' on the
polymorphic object 'Categorizable#categorizable'.
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/reflection.rb:187:in `check_validity!'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/associations/has_many_through_association.rb:6:in
`initialize'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/associations.rb:1032:in `new'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/associations.rb:1032:in `categorizables'
from (irb):9
thanks everybody for the help.
I got it working now with the plugin has_many_polymorphs.
Would have been nice to get it working with standard rails, too. But
also I was googling and used some reference books like the rails way
or agile webdevelopment with rails I could find a working solution...