Use auto_increment before data is stored

Hello,

I have 2 models (category with id(auto_increment), name and item with
id(auto_increment), category_id, name).
But I want to store the data with only one form that creates a new
category and the corresponding items at the same time.

My problem is now that I don’t know how to tell Ruby to fetch the
auto_increment id of the category table and then use it for the
category_id of the item table.

Any ideas?

Phi

I recently found
http://wiki.rubyonrails.com/rails/pages/HowToUpdateMultipleAssociatedFormElementsOnOnePage

But the difference is that this only handles updates. When updating an
object, the auto_increment id already exists. My problem is that this id
doesn’t exist so far.
Any ideas?

Phi wrote:

Hello,

I have 2 models (category with id(auto_increment), name and item with
id(auto_increment), category_id, name).
But I want to store the data with only one form that creates a new
category and the corresponding items at the same time.

My problem is now that I don’t know how to tell Ruby to fetch the
auto_increment id of the category table and then use it for the
category_id of the item table.

Any ideas?

If
category = Category.create(params[:category])
item = Item.create(params[:item])
item.category = category

doesn’t work, try

category = Category.create(params[:category])
category.save
item = Item.create(params[:item])
item.category = category
category.save

Alan

Phi

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs