2011-06-23 40 views
0

我正在使用accept_nested_attributes保存记录。我想要访问创建的子记录的ID。 例如params[:client]已将subscriptions_attributes作为nested_attributes从表单中删除。 Clienthas_many :subscriptions。 当我打电话给@client.save。它将节省client以及subscriptions。我想访问已插入订阅的ids检索插入了嵌套属性的记录

一个解决方案我是保存记录,然后再收集后保存,然后(after_ids - before_ids)

有任何防护栏方式或傻瓜证明的方法来做到这一点之前收集subscription_ids

回答

1

这可以是一种替代方法: 您可以在保存客户端之前节省时间,然后检索在该时间之后创建的记录。 例如 before_create_time = Time.now

然后,......降低了查询节省

inserted_subscriptions = @client.subscriptions.where('created_at > ?', before_create_time)

+0

感谢后ATLEAST ... :) – Pravin