这是我在这里的第一个问题,所以希望它能解决。我已经四处寻找类似问题的人,但到目前为止还没有找到任何东西。我确定这很简单,但我现在看不到它!
我正在尝试在 rails 3.2.11 中保存模型的嵌套属性,但该操作仅在创建嵌套对象时有效,但在更新它时无效。
这是我使用的 2 个模型的代码
class Property < ActiveRecord::Base
has_many :opening_times
accepts_nested_attributes_for :opening_times, :allow_destroy => true
attr_accessible :opening_times_attributes
...
class OpeningTime < ActiveRecord::Base
belongs_to :property
attr_accessible :start_date, :end_date
attr_accessible :day, :start_time, :end_time
attr_writer :day, :start_time, :end_time
before_save :set_dates
...
def set_dates
day = Date.parse(@day)
start_time = Time.parse(@start_time)
end_time = Time.parse(@end_time)
start_date = "#{day.day}/#{day.month}/#{day.year} #{start_time.hour}:#{start_time.min}"
self.start_date = DateTime.parse(start_date)
end_date = "#{day.day}/#{day.month}/#{day.year} #{end_time.hour}:#{end_time.min}"
self.end_date = DateTime.parse(end_date)
end
因此,当我尝试使用 rails 控制台通过属性创建开放时间时,它可以工作:
1.9.3p125 :006 > p = Property.find(9)
1.9.3p125 :006 > p.opening_times_attributes = [{"day"=>"27/02/2013", "start_time"=>"11:30", "end_time"=>"12:30", "_destroy"=>"false"}]
=> [{"day"=>"27/02/2013", "start_time"=>"11:30", "end_time"=>"12:30", "_destroy"=>"false"}]
1.9.3p125 :007 > p.save!
(0.1ms) begin transaction
Suburb Load (0.2ms) SELECT "suburbs".* FROM "suburbs" WHERE "suburbs"."name" = 'BARREN GROUNDS' LIMIT 1
SQL (0.6ms) INSERT INTO "opening_times" ("end_date", "property_id", "start_date") VALUES (?, ?, ?) [["end_date", Wed, 27 Feb 2013 12:30:00 UTC +00:00], ["property_id", 9], ["start_date", Wed, 27 Feb 2013 11:30:00 UTC +00:00]]
(2.8ms) commit transaction
=> true
但是当我尝试更新现有的嵌套对象(在哈希中传递 id)时,它什么也没做
1.9.3p125 :037 > p.opening_times
=> [#<OpeningTime id: 12, property_id: 9, start_date: "2013-02-27 11:00:00", end_date: "2013-02-27 13:00:00">]
1.9.3p125 :038 > p.opening_times_attributes = [{"day"=>"27/02/2013", "start_time"=>"11:30", "end_time"=>"12:30", "_destroy"=>"false", "id"=>12}]
=> [{"day"=>"27/02/2013", "start_time"=>"11:30", "end_time"=>"12:30", "_destroy"=>"false", "id"=>12}]
1.9.3p125 :039 > p.save!
(0.1ms) begin transaction
Suburb Load (0.2ms) SELECT "suburbs".* FROM "suburbs" WHERE "suburbs"."name" = 'BARREN GROUNDS' LIMIT 1
(0.1ms) commit transaction
=> true
1.9.3p125 :040 > exit
根据我到目前为止所阅读的内容(即http://archives.ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes),这应该适用于两种操作。
知道我做错了什么吗?
谢谢!
[编辑]
根据@jvnill 的建议,更新时似乎没有调用 before_save 回调。我添加了一种解决方法,以在更新任何字段然后它起作用时显式调用 set_dates。
def day=(day)
@day = day
set_dates unless (@day.blank? || @start_time.blank? || @end_time.blank?)
end
def start_time=(start_time)
@start_time = start_time
set_dates unless (@day.blank? || @start_time.blank? || @end_time.blank?)
end
def end_time=(end_time)
@end_time = end_time
set_dates unless (@day.blank? || @start_time.blank? || @end_time.blank?)
end
它并没有完全解决问题,因为现在验证不能无缝地工作,而且似乎我不得不手动完成 AR 应该做的工作。