Ruby on rails Rails 3多态关联引发名称错误

Ruby on rails Rails 3多态关联引发名称错误,ruby-on-rails,polymorphic-associations,Ruby On Rails,Polymorphic Associations,我有以下两种型号: class FuzzyDate < ActiveRecord::Base belongs_to :fuzzy_dateable, :polymorphic => true end class Device < ActiveRecord::Base has_one :received_date, :as => :fuzzy_dateable end 让我们把控制台打开,好吗 >> d = Device.create(:meid =

我有以下两种型号:

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

class Device < ActiveRecord::Base
  has_one :received_date, :as => :fuzzy_dateable
end
让我们把控制台打开,好吗

>> 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 => Date.today, :fuzz => 3)
=> #<FuzzyDate id: 1, date: "2010-12-15", fuzz: 3, fuzzy_dateable_id: nil, fuzzy_dateable_type: nil, created_at: "2010-12-16 06:53:04", updated_at: "2010-12-16 06:53:04">
>> fd.fuzzy_dateable = d
=> #<Device id: 1, meid: "A", created_at: "2010-12-16 06:52:55", updated_at: "2010-12-16 06:52:55">
>> fd
=> #<FuzzyDate id: 1, date: "2010-12-15", fuzz: 3, fuzzy_dateable_id: 1, fuzzy_dateable_type: "Device", created_at: "2010-12-16 06:53:04", updated_at: "2010-12-16 06:53:04">

啊!我在谷歌上搜索过很多次,我发现的答案都是错误地使用复数模型名,但这里的情况并非如此。我花了几个晚上的时间来讨论这个问题,所以如果您能提供任何指导,我将不胜感激。

这里的问题是,在您的设备型号中,您可以将关联命名为:received\u date,并且默认情况下,它将查找ReceivedDate型号,除非您另有说明

我想说你可以从这些解决方案中选择一个

第一号

class设备:fuzzy\u dateable,:class\u name=>“FuzzyDate”
结束
二号

class设备:fuzzy\u可日期
结束

Bingo!就这样。谢谢你的帮助。
>> 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 => Date.today, :fuzz => 3)
=> #<FuzzyDate id: 1, date: "2010-12-15", fuzz: 3, fuzzy_dateable_id: nil, fuzzy_dateable_type: nil, created_at: "2010-12-16 06:53:04", updated_at: "2010-12-16 06:53:04">
>> fd.fuzzy_dateable = d
=> #<Device id: 1, meid: "A", created_at: "2010-12-16 06:52:55", updated_at: "2010-12-16 06:52:55">
>> fd
=> #<FuzzyDate id: 1, date: "2010-12-15", fuzz: 3, fuzzy_dateable_id: 1, fuzzy_dateable_type: "Device", created_at: "2010-12-16 06:53:04", updated_at: "2010-12-16 06:53:04">
>> d.received_date
NameError: uninitialized constant Device::ReceivedDate
        from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:1199:in `compute_type'
        from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/reflection.rb:162:in `send'
        from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/reflection.rb:162:in `klass'
        from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/reflection.rb:198:in `quoted_table_name'
        from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/associations/has_one_association.rb:97:in `construct_sql'
        from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/associations/has_one_association.rb:7:in `initialize'
        from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/associations.rb:1450:in `new'
        from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/associations.rb:1450:in `received_date'
        from (irb):5
>> 
class Device < ActiveRecord::Base
  has_one :received_date, :as => :fuzzy_dateable, :class_name => "FuzzyDate"
end
class Device < ActiveRecord::Base
  has_one :fuzzy_date, :as => :fuzzy_dateable
end