开发者

Retrieve records inserted with nested attributes

开发者 https://www.devze.com 2023-03-15 07:44 出处:网络
I am using accept_nested_attributes to save records. I want to access ids of the child records created.

I am using accept_nested_attributes to save records. I want to access ids of the child records created. e.g. params[:client] has subscriptions_attributes coming as nested_attributes from the form. Client has_many :subscriptions. When I'll call @client.save. It will save client as well as subscriptions. I want to access the ids of the subscriptions inserted.

One solution I have is to collect subscr开发者_高级运维iption_ids before saving the records and then collecting again after save and then (after_ids - before_ids)

Is there any rails way or fool proof method to do this?


This can be one alternative: You can save the time just before saving the client and then retrieve the records created after that time. E.g. before_create_time = Time.now

Then, after saving

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

0

精彩评论

暂无评论...
验证码 换一张
取 消