2010-12-16 10 views
2

Ich habe folgende 2 Modelle erhöhen:Rails 3 polymorphe Vereinigung eine Nameerror

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

class Device < ActiveRecord::Base 
    has_one :received_date, :as => :fuzzy_dateable 
end 

mit folgendem Schema:

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 

Lassen Sie uns die Konsole anwerfen, so sind wir?

>> 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"> 

Ok, so weit, so gut. Lass uns aus der anderen Richtung gehen:

>> 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 
>> 

Doh! Ich habe eine ganze Reihe von Google-Werbungen gemacht, und die Antworten, die ich gefunden habe, gehen alle auf die falsche Verwendung von Pluralmodellnamen zurück, was hier nicht der Fall ist. Ich habe ein paar Abende damit verbracht, meinen Kopf dagegen zu schlagen, deshalb würde jede Anleitung sehr geschätzt werden.

Antwort

7

Das Problem hier ist, dass Sie in Ihrem Gerätemodell die Assoziation: received_date benennen, die standardmäßig nach einem ReceivedDate-Modell sucht, sofern Sie nichts anderes angeben.

Ich würde sagen, Sie können aus einer dieser Lösungen wählen.

No. 1

class Device < ActiveRecord::Base 
    has_one :received_date, :as => :fuzzy_dateable, :class_name => "FuzzyDate" 
end 

No. 2

class Device < ActiveRecord::Base 
    has_one :fuzzy_date, :as => :fuzzy_dateable 
end 
+0

Bingo! Das war's. Danke für Ihre Hilfe. –

+0

das ist es. Vielen Dank – yek

Verwandte Themen