Cameron Booth
2006-Jul-09 16:08 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/.
Roland Mai
2006-Jul-09 16:37 UTC
[Rails] Re: Can the has_many create() method return an ID?
This should work, because the create returns the object it created cookbook.recipes.create(data).id roland -- Posted via http://www.ruby-forum.com/.
Travis Michel
2006-Jul-13 01:30 UTC
[Rails] Re: Can the has_many create() method return an ID?
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 On 7/9/06, Roland Mai <roland.mai@gmail.com> wrote:> This should work, because the create returns the object it created > > cookbook.recipes.create(data).id > > roland > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- p [151,40,154,157,166,145,40,162,165,142,171].collect { |ii| eval ''"\\''+ii.to_s+''"'' }.join
Travis Michel
2006-Jul-13 02:02 UTC
[Rails] Re: Can the has_many create() method return an ID?
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
Pete Yandell
2006-Jul-13 02:17 UTC
[Rails] Re: Can the has_many create() method return an ID?
That''s definitely not normal Rails behaviour. What version of Rails are you running, what system, what database, etc.? On 13/07/2006, at 11:30 AM, Travis Michel wrote:> 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 > > > > On 7/9/06, Roland Mai <roland.mai@gmail.com> wrote: >> This should work, because the create returns the object it created >> >> cookbook.recipes.create(data).id >> >> roland >> >> -- >> Posted via http://www.ruby-forum.com/. >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > -- > p [151,40,154,157,166,145,40,162,165,142,171].collect { |ii| eval > ''"\\''+ii.to_s+''"'' }.join > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Travis Michel
2006-Jul-13 02:45 UTC
[Rails] Re: Can the has_many create() method return an ID?
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
David Rose
2006-Jul-13 19:37 UTC
[Rails] Re: Can the has_many create() method return an ID?
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 Rose (doppler) On 7/12/06, Travis Michel <meshac.ruby@gmail.com> wrote:> > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060713/2db7fc9a/attachment.html
Pete Yandell
2006-Jul-14 01:21 UTC
[Rails] Re: Can the has_many create() method return an ID?
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 Yandell http://9cays.com/
Travis Michel
2006-Jul-14 08:44 UTC
[Rails] Re: Can the has_many create() method return an ID?
Wow, thanks Pete, Your awesome. On 7/13/06, Pete Yandell <pete@notahat.com> wrote:> > 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 Yandell > > http://9cays.com/_______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- p [151,40,154,157,166,145,40,162,165,142,171].collect { |ii| eval ''"\\''+ii.to_s+''"'' }.join -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060714/59efd3e4/attachment-0001.html