# Can the has\_many create() method return an ID?

**URL:** <https://www.ruby-forum.com/t/can-the-has-many-create-method-return-an-id/97689>\
**Category:** Rails\
**Created:** [July 9, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/can-the-has-many-create-method-return-an-id/97689 "2006-07-09T18:07:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cdb](https://www.ruby-forum.com/u/cdb)\
**Post date:** [July 9, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/can-the-has-many-create-method-return-an-id/97689/1 "2006-07-09T18:07:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cdb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cdb](https://www.ruby-forum.com/u/cdb)\
**Post date:** [July 9, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/can-the-has-many-create-method-return-an-id/97689/2 "2006-07-09T18:37:26Z")

</div>

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

cookbook.recipes.create(data).id

roland

---

<div class="post-metadata">

**Author:** ![cdb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cdb](https://www.ruby-forum.com/u/cdb)\
**Post date:** [July 13, 2006, 3:32am UTC](https://www.ruby-forum.com/t/can-the-has-many-create-method-return-an-id/97689/3 "2006-07-13T03:32:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cdb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cdb](https://www.ruby-forum.com/u/cdb)\
**Post date:** [July 13, 2006, 4:03am UTC](https://www.ruby-forum.com/t/can-the-has-many-create-method-return-an-id/97689/4 "2006-07-13T04:03:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cdb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cdb](https://www.ruby-forum.com/u/cdb)\
**Post date:** [July 13, 2006, 4:18am UTC](https://www.ruby-forum.com/t/can-the-has-many-create-method-return-an-id/97689/5 "2006-07-13T04:18:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cdb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cdb](https://www.ruby-forum.com/u/cdb)\
**Post date:** [July 13, 2006, 4:46am UTC](https://www.ruby-forum.com/t/can-the-has-many-create-method-return-an-id/97689/6 "2006-07-13T04:46:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cdb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cdb](https://www.ruby-forum.com/u/cdb)\
**Post date:** [July 13, 2006, 9:58pm UTC](https://www.ruby-forum.com/t/can-the-has-many-create-method-return-an-id/97689/7 "2006-07-13T21:58:40Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![cdb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cdb](https://www.ruby-forum.com/u/cdb)\
**Post date:** [July 14, 2006, 10:46am UTC](https://www.ruby-forum.com/t/can-the-has-many-create-method-return-an-id/97689/8 "2006-07-14T10:46:19Z")

</div>

Wow, thanks Pete, Your awesome.

---

<div class="post-metadata">

**Author:** ![cdb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cdb](https://www.ruby-forum.com/u/cdb)\
**Post date:** [July 14, 2006, 3:22am UTC](https://www.ruby-forum.com/t/can-the-has-many-create-method-return-an-id/97689/9 "2006-07-14T03:22:47Z")

</div>

From the How To Use SQLite in Rails page (http://  
[wiki.rubyonrails.org/rails/pages/HowtoUseSQLite](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/\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_](http://9cays.com/ _______________________________________________ )  
Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
