class Store < ActiveRecord::Base
belongs_to :user
has_many :products, :as => :imageable
end
class User < ActiveRecord::Base
has_one :store
has_many :products, :as => imageable
end
class Product < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
和我进行迁移:
class AddImageableToProducts < ActiveRecord::Migration
def self.up
change_table :products do |t|
t.references :imageable, :polymorphic => true
end
end
def self.down
remove_column :products, :imageable
end
end
当我尝试运行我的应用程序我得到:未定义的局部变量或方法'成像' 和我不不知道我错过了什么。我很感激,如果有人可以帮助。谢谢
谢谢山姆。我使用的是Rails 3,但我不知道需要单独的lib文件或在application.rb中有config.autoload_paths。我阅读了rubyonrails.org上的多态关联,并没有提到任何这些。你知道我在哪里可以读到你对我的建议吗?谢谢你一堆 – railslearner 2011-04-04 17:40:05
我尝试了你提到的一切,而且我仍然遇到同样的错误。多态协会是应该实现这么复杂吗? – railslearner 2011-04-04 17:50:59
试试这个:http://railscasts.com/episodes/154-polymorphic-association – 2011-04-05 06:14:29