0
我在引入多態關聯之後在RailsApp中遇到了一個問題。該應用由可以具有多個屬性的團隊(以及其他模型)組成。所以我也跟着railscasts事件以及與此想出了:Failling在引入多態關聯之後保存
應用程序/模型/ attribute.rb
class Attribute < ActiveRecord::Base
attr_accessible :content
belongs_to :attributable, polymorphic: true
end
應用程序/模型/ team.rb
class Team < ActiveRecord::Base
has_many :attributes, as: :attributable
accepts_nested_attributes_for :attributes
end
這樣做之後,我遇到了這個在IRB會議中:
irb(main):001:0> t = Team.new(name: "Test")
=> #<Team id: nil, name: "Test", created_at: nil, updated_at: nil, scopes_mask: nil>
irb(main):002:0> t.save
(0.3ms) begin transaction
commit transaction
=> true
irb(main):003:0> t.save
(0.4ms) begin transaction
rollback transaction
NoMethodError: Attribute Load (0.6ms) SELECT "attributes".* FROM "attributes" WHERE "attributes"."attributable_id" = 1 AND "attributes"."attributable_type" = 'Team'
undefined method `keys' for []:ActiveRecord::Relation
所以錯誤只發生在先前記錄的記錄上。第一次保存的建立記錄和屬性的做法非常好,正如加載記錄一樣。
我希望對這種情況有所幫助。 此致