Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 Rails4:从子窗体创建父窗体时访问父窗体中的子对象_Ruby On Rails 4_Activerecord_Ruby On Rails 3.2_Associations_Rails Activerecord - Fatal编程技术网

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