1

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 ShipClasses 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

1
  • Each ShipEntry belongs to only one ShipClass? Commented Jul 7, 2011 at 10:17

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Yea. this was my exact thought but knowing rails and its wonders I thought it might have had some amazing method to achieve this. Cheers for responding tho!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.