我正在使用accept_nested_attributes
来保存记录。我想访问创建的子记录的ID。
例如来自表单的params[:client]
subscriptions_attributes
为nested_attributes
。 Client
has_many :subscriptions
。
当我打电话给@client.save
时。它将保存client
以及subscriptions
。我想访问插入的订阅的ids
。
我的一个解决方案是在保存记录之前收集subscription_ids
,然后在保存之后再次收集,然后(after_ids - before_ids)
是否有任何导轨方式或万无一失的方法来执行此操作?
答案 0 :(得分:1)
这可以是另一种选择:
您可以在保存客户端之前保存时间,然后检索在该时间之后创建的记录。
例如。
before_create_time = Time.now
然后,保存后
inserted_subscriptions = @client.subscriptions.where('created_at > ?', before_create_time)