【发布时间】:2013-05-23 18:30:55
【问题描述】:
我可能遗漏了一些明显的东西,但是当我尝试保存一个对象时,它不会在数据库中更新。我可以插入对象。这是我的模型。我可以在用户模型上正确地做所有事情,我只是在争议模型上遇到了问题。
class Dispute < ActiveRecord::Base
attr_accessible :reason
belongs_to :user
end
class User < ActiveRecord::Base
attr_accessible :name, :email
has_many :disputes
end
然后我尝试...
d = Dispute.new
d.save # This works, it is inserted correctly
BEGIN
INSERT
COMMIT
d.reason = "This is a reason"
d.save # This doesn't work
BEGIN
COMMIT
d.reason = "This is a different reason"
d.changed?
true
d.save
BEGIN
COMMIT
不知道我错过了什么。我确实简化了一点。我在这个项目中有许多其他对象,它们都工作得很好。
d.update_attributes( :reason => "This is a reason" )
这也是同样的行为,它不更新数据库中的记录,但它返回 true。
【问题讨论】:
-
而
.save也返回true? -
在
d.reason = "This is a reason"和d.save之后,做Dispute.find_by_reason('This is a reason')能找到记录吗? -
@Dogbert - 是的,它确实返回 true。
-
@garbagecollection 当我进行搜索时,我得到 nil ,这是有道理的,因为它没有在数据库中更新。
-
@Karl,
Dispute.new(reason: "...").save!会发生什么?
标签: ruby-on-rails activerecord