Sorry, still not working…
c = Category.find(943302162)
=> #<Category id: 943302162, name: “computer_science”, description:
“Informatik”, parent_id: 2061879279, created_at: “2008-03-18
09:53:21”, updated_at: “2008-03-18 09:53:21”>
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
What I have now…
The migration files:
class CreateCategories < ActiveRecord::Migration
def self.up
create_table :categories do |t|
t.string :name, :description
t.integer :parent_id
t.timestamps
end
end
def self.down
drop_table :categories
end
end
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.timestamps
end
end
def self.down
drop_table :users
end
end
class CreateCategorizedItems < ActiveRecord::Migration
def self.up
create_table :categorized_items do |t|
t.references :categorizable, :null => false, :polymorphic =>
true
t.references :category, :null => false
t.timestamps
end
end
def self.down
drop_table :categorized_items
end
end
The models:
class Category < ActiveRecord::Base
acts_as_tree
has_many :categorized_items
has_many :categorizables, :through => :categorized_items, :source
=> :categorizable
end
class User < ActiveRecord::Base
has_many :categorized_items, :as=>:categorizable # could be has_one
end
class CategorizedItem < ActiveRecord::Base
belongs_to :category
belongs_to :categorizable, :polymorphic=>true
end
And how would I do something like the following with the associations
above?
c = Category.create
u = User.create
u.categories << Category
Thanks for your patience!
Feurio