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:

cookbook.recipes.create(data)

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!

Cameron

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

cookbook.recipes.create(data).id

roland

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

Post.count
=> 0

Post.new.id
=> nil

Post.create.id
=> 0

Post.create.id
=> 0

Post.count
=> 2

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

and

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

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

@post.categories.size
=> 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
)
end

cheers,

  • 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
fact
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
to
insure that it returns the desired record:
#my class is called Itinerary, and uses the created_at convention,
obviously
def after_create
write_attribute( ‘id’, Itinerary.find( :first, :select => ‘id’,
:conditions => [“name = ? and created_at = ?”, self.name,
self.created_at] ).id
)
end

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

David R. (doppler)

Wow, thanks Pete, Your awesome.

From the How To Use SQLite in Rails page (http://
wiki.rubyonrails.org/rails/pages/HowtoUseSQLite):

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.

http://9cays.com/_______________________________________________
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails