Ruby on rails Rails 4.1缺少属性错误
我有一个模型Ruby on rails Rails 4.1缺少属性错误,ruby-on-rails,activerecord,ruby-on-rails-4,associations,ruby-on-rails-4.1,Ruby On Rails,Activerecord,Ruby On Rails 4,Associations,Ruby On Rails 4.1,我有一个模型 PaypalTransactionResponse(id: integer, paypal_transaction_id: uuid, response: text, created_at: datetime, updated_at: datetime) class PaypalTransaction < ActiveRecord::Base has_many :responses, class_name: "PaypalTransactionResponse",
PaypalTransactionResponse(id: integer, paypal_transaction_id: uuid, response: text, created_at: datetime, updated_at: datetime)
class PaypalTransaction < ActiveRecord::Base
has_many :responses, class_name: "PaypalTransactionResponse", dependent: :destroy
belongs_to :response, class_name: "PaypalTransactionResponse", foreign_key: "response"
..
end
class PaypalTransactionResponse < ActiveRecord::Base
has_one :paypal_transaction
belongs_to :paypal_transaction
..
end
PaypalTransactionResponse(id:integer,paypal\u transaction\u id:uuid,response:text,创建时间:datetime,更新时间:datetime)
类PaypalTransaction
在Rails 4.0.5上,以下代码运行良好。在Rails 4.1.1中,它没有
p = PaypalTransaction.first
p.responses.create(response: "some response text")
pry(#<PaypalTransaction>)> p.responses.create(response: "some response text")
SQL (0.4ms) INSERT INTO "paypal_transaction_responses" ("created_at", "paypal_transaction_id", "response", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", "2014-05-10 13:54:37.512642"], ["paypal_transaction_id", "048267d9-2d47-4f0b-bc67-e206e296179d"], ["response", "some response text"], ["updated_at", "2014-05-10 13:54:37.512642"]]
ActiveModel::MissingAttributeError: can't write unknown attribute `paypal_transaction_response_id'
from /Users/me/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active_record/attribute_methods/write.rb:72:in `write_attribute'
p=PaypalTransaction.first
p、 响应。创建(响应:“一些响应文本”)
pry(#)>p.responses.create(响应:“一些响应文本”)
SQL(0.4ms)插入到“paypal交易响应”(“创建时间”、“paypal交易id”、“响应”、“更新时间”)值($1,$2,$3,$4)中,返回“id”[[“创建时间”、“2014-05-10 13:54:37.512642”]、[“paypal交易id”、“048267d9-2d47-4f0b-bc67-e206e296179d”]、[“响应”、“一些响应文本”]、[“更新时间”,"2014-05-10 13:54:37.512642"]]
ActiveModel::MissingAttributeError:无法写入未知属性“paypal\u事务\u响应\u id”
来自/Users/me/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active\u record/attribute\u methods/write.rb:72:in'write\u attribute'
此外,我没有一个paypal\u transaction\u response\u id
属性。我猜这里有一些不好的魔法,因为它试图猜测一些不存在的属性
我在Rails 4.1变更日志中没有发现任何可能暗示此问题的更改
我能做什么?您的
有一个问题:paypal\u交易关联最有可能是问题的原因
它期望在您的PaypalTransaction
类上有一个外键属性指向PaypalTransactionResponse
,并猜测该属性称为paypal\u transaction\u response\u id
如果你定义你的有一个类似的:
has_one :paypal_transaction, foreign_key: "response"
那么,希望这能解决问题
rails 4.1现在失败的原因是不再支持编写任意属性(您不能执行my_obj[:my_att]=42
)。我怀疑代码在4.0.5中没有出错,但可能没有完全按照您的预期工作——或者至少没有正常工作