开发者

Ruby on Rails 3 - nested model forms

I am using RoR 3 to create a new website for myself. I have 3 models like so:

Ship model has many ShipClasses
Ship model has many ShipEntries
ShipEntry belongs to Ship
ShipEntry belongs to ShipClass

When I am creating the Ship I generated nested forms for ShipClasses and ShipEntries. The problem I have is when creating a ShipEntry it should have one of the new ShipClasses assigned to it but at the moment when the Ship is created I have to go back in and edit the Ship and assign the ShipC开发者_如何学Golasses to the ShipEntry due to the new ShipClasses not yet having been created in the DB. Is it possible at the creation of the ship to assign the newly created ShipClasses to the ShipEntry?

I hope someone gets what I mean, if you have any trouble understanding this let me know and i'll try to explain further.

Cheers

Eef


You can achieve what you want to do with active callbacks.

You could use something like after_create to link both.

after_create :link_ship_entry_and_classes

def link_ship_entry_and_classes
  # whatever you need to do here
end

You can read everything there is to know about AC : http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

Hope this helps!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜