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