小编Con*_*nia的帖子

Rails 3多态关联引发NameError

我有以下两种型号:

class FuzzyDate < ActiveRecord::Base
  belongs_to :fuzzy_dateable, :polymorphic => true
end

class Device < ActiveRecord::Base
  has_one :received_date, :as => :fuzzy_dateable
end
Run Code Online (Sandbox Code Playgroud)

使用以下架构:

  create_table "devices", :force => true do |t|
    t.string   "meid"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "fuzzy_dates", :force => true do |t|
    t.date     "date"
    t.integer  "fuzz"
    t.integer  "fuzzy_dateable_id"
    t.string   "fuzzy_dateable_type"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
Run Code Online (Sandbox Code Playgroud)

让我们开启控制台,好吗?

>> d = Device.create(:meid => "A")
=> #<Device id: 1, meid: "A", created_at: "2010-12-16 06:52:55", updated_at: "2010-12-16 06:52:55">
>> fd = FuzzyDate.create(:date => …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails polymorphic-associations

2
推荐指数
1
解决办法
1740
查看次数