Can the has_many create() method return an ID?

Hi all,

I have a has_many relationship, Cookbook :has_many Recipes

If I want to create a new recipe in my cookbook I do:

It would be nice if I could then easily get the id of the new recipe,
but the create() method doesn’t seem to return anything. Any ideas how?

Thanks in advance!


This should work, because the create returns the object it created


I have some evidence of an id not being returned on create (from the

=> 0
=> nil
=> 0
=> 0

=> 2

Post.find(:all).collect { |post| }
=> [1, 2]


@post.categories.create({:name => ‘cc1’}).id
=> 0

@post.categories.create({:name => ‘cc2’}).id
=> 0

=> 2

Are there any work arounds for this?

  • trav

As a work around, this should do the trick, but it requires attributes
that in combination are equivalent (or close) to the id column.
Therefore its a bit iffy.

class Post < ActiveRecord::Base

def after_create
write_attribute( ‘id’, Post.find( :first, :select => ‘id’,
:conditions => [ # attributes # ] ).id


  • trav

That’s definitely not normal Rails behaviour. What version of Rails
are you running, what system, what database, etc.?

activerecord (1.14.2)
activesupport (1.3.1)
rails (1.1.2)
sqlite3-ruby (1.1.0)

sqlite3 -version => 3.1.3

OS-X 10.4.7

I’ve been hanging around this problem for a while now, if you want to
point me in the right direction I’ll look at the source and see if I
can do anything about it.

  • trav

I’m hung up on this same problem, on the same platform, save for the
that I’m using rails 1.1.4 and corresponding versions of its libraries.

I took Travis’ suggestion, but extended it a bit, to make a good effort
insure that it returns the desired record:
#my class is called Itinerary, and uses the created_at convention,
def after_create
write_attribute( ‘id’, Itinerary.find( :first, :select => ‘id’,
:conditions => [“name = ? and created_at = ?”,,
self.created_at] ).id

This works, but it’s pretty ugly, and not 100% safe: what if two users
an Itinerary named “Trip to Houston” within one second of each other?

David R. (doppler)

Wow, thanks Pete, Your awesome.

From the How To Use SQLite in Rails page (http://

Q: It seems is that ActiveRecord?s save method doesn?t set the id
of the inserted row after saving the record to a SQLite database.
Is this an issue with SQLite or with ActiveRecord?

A: Make sure you have swig installed before installing sqlite3-
ruby! See the question immediately above this one for instructions.
I?ve had the exact problem and installing swig then re-install
sqlite3-ruby did fix it. One note: I believe sqlite3-ruby is the
preferred module (sudo gem install sqlite3-ruby).
and the relevant bit on installing swig:

On Mac OS X 10.4 Tiger, try uninstalling the gem (sudo gem
uninstall sqlite3), then use DarwinPorts or Fink to install SWIG
(sudo port install swig), making sure SWIG is in your PATH, then re-
install the SQLite3 gem (sudo gem install sqlite3).

Pete Y.
Rails mailing list
[email protected]

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