Ruby on rails 4 Rails4:从子窗体创建父窗体时访问父窗体中的子对象
我也有这样的联想 交付模式Ruby on rails 4 Rails4:从子窗体创建父窗体时访问父窗体中的子对象,ruby-on-rails-4,activerecord,ruby-on-rails-3.2,associations,rails-activerecord,Ruby On Rails 4,Activerecord,Ruby On Rails 3.2,Associations,Rails Activerecord,我也有这样的联想 交付模式 class Delivery < ActiveRecord::Base belongs_to :schedule, inverse_of: :deliveries accepts_nested_attributes_for :schedule end 现在,在保存交货的同时,我也接受了一组计划细节 当它保存交货和时间表时,在创建时间表模型后调用回调,当它试图找出交货时,最后给出nil值。 因为我接受它作为嵌套属性的一部分,所以传递对象应该对它可用,但它
class Delivery < ActiveRecord::Base
belongs_to :schedule, inverse_of: :deliveries
accepts_nested_attributes_for :schedule
end
现在,在保存交货的同时,我也接受了一组计划细节
当它保存交货和时间表时,在创建时间表模型后调用回调,当它试图找出交货时,最后给出nil
值。
因为我接受它作为嵌套属性的一部分,所以传递对象应该对它可用,但它仍然给出nil
值
我有什么遗漏吗?谢谢。在我看来,Rails正在尝试在提交相关记录之前进行数据库调用
还有一个额外的回调,您可以在提交后查看(代替创建后的),该回调将在记录提交到您正在使用的任何数据库后触发
这意味着您的deliveries.last
应该返回为非空。您还可以使用self.reload
强制在模型上查找数据库,尽管这可能会导致意外行为
class Schedule < ActiveRecord::Base
has_many :deliveries, inverse_of: :schedule
include PushUpdates
def offer
deliveries.last.try(:offer)
end
end
module PushUpdates
extend ActiveSupport::Concern
included do
after_create {update_client_store :create unless Rails.env.test? }
after_update {update_client_store :update unless Rails.env.test? }
end
def update_client_store(operation)
...
self.offer
end
end