Hello, I''m using the seeds.rb to create some user records: 100.times do user = User.new user.username = Faker::Name.first_name user.email = user.username+"@example.com" user.save! end This works just fine and the users have been created after I ran db:seeds but what''s not working is: after_create :write_comments protected def write_comments comment = Comment.new comment.text = "some text" comment.user = self comment.save! end There''s not one single comment in the database but when I create a user using the console the comment is created after I save the user. Anyone knows what''s wrong here? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
i think self is a reserved word! shouldnt it be current_user? On Sun, Sep 12, 2010 at 8:23 AM, Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > > I''m using the seeds.rb to create some user records: > 100.times do > user = User.new > user.username = Faker::Name.first_name > user.email = user.username+"@example.com" > user.save! > end > > This works just fine and the users have been created after I ran > db:seeds but what''s not working is: > > after_create :write_comments > > protected > def write_comments > comment = Comment.new > comment.text = "some text" > comment.user = self > comment.save! > end > > There''s not one single comment in the database but when I create a user > using the console the comment is created after I save the user. > > Anyone knows what''s wrong here? > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Heinz Strunk wrote:> Hello, > > I''m using the seeds.rb to create some user records: > 100.times do > user = User.new > user.username = Faker::Name.first_name > user.email = user.username+"@example.com" > user.save! > endThis isn''t directly a solution to your problem, but... Is this for testing? If so, I recommend using factories, not seeds. seeds.rb is for application seed data (such as tables of countries or shipping rates), not what you''re apparently doing here. ...but why do your tests need 100 records to begin with? Normally, you should just call the factory within each test to create the (1 or 2) records you need for that test. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
@brito: self always worked cause it''s the instance of the current object which I wanna use so that should be fine. @Marnen: Actually they''re not really users but NPCs which I need for various things and I need a lot of them to populate the world. Therefore it''s not for test but to populate and I don''t think factories are good for so many objects, right? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Please quote when replying; otherwise, it gets difficult to follow the discussion. Heinz Strunk wrote:> @Marnen: Actually they''re not really users but NPCs which I need for > various things and I need a lot of them to populate the world. > > Therefore it''s not for test but to populateSo this is actual production data?> and I don''t think factories > are good for so many objects, right?Factories should be fine for creating so many objects -- but they''re not for creating production data. If it''s production data, create it all and use seeds. If it''s test data, create only the few records you need and use factories. [and, from earlier post]> This works just fine and the users have been created after I ran > db:seeds but what''s not working is: > > after_create :write_comments > > protected > def write_comments > comment = Comment.new > comment.text = "some text" > comment.user = self > comment.save! > endIt''s not appropriate to use an after_create for this sort of thing, I think. In your 100.times block, just create a comment when you create each user. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser wrote:> Please quote when replying; otherwise, it gets difficult to follow the > discussion. > > > So this is actual production data?Yes, it is.> > It''s not appropriate to use an after_create for this sort of thing, I > think. In your 100.times block, just create a comment when you create > each user.Makes sense for comments but there but there''s stuff like attributes etc. which needs to be created even when a user is created from the web interface. Do you know why the code above doesn''t work? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sunday 12 September 2010, Heinz Strunk wrote:> after_create :write_comments > > protected > def write_comments > comment = Comment.new > comment.text = "some text" > comment.user = self > comment.save! > end > > There''s not one single comment in the database but when I create a > user using the console the comment is created after I save the user.Is the after_create callback ever executed? Add a puts to make sure.> Anyone knows what''s wrong here?I wouldn''t write code like that to begin with. This is much nicer: def write_comments user.comments.create!(:text => ''some text'') end Michael -- Michael Schuerig mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
For some stupid reason it''s working now. My guess is a Windows reboot did the magic ;) It is indeed much nicher! Thanks everybody! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
jojo, now i see what you are doing, is just that i didnt saw the after create the first time, you are using the self reserved word indeed to refer to the created user in the after create, have you tried that before? why not put the function in the loop after the user.save ? i imagine that that it fail quietly? On Mon, Sep 13, 2010 at 12:03 PM, Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> @brito: self always worked cause it''s the instance of the current object > which I wanna use so that should be fine. > > @Marnen: Actually they''re not really users but NPCs which I need for > various things and I need a lot of them to populate the world. > > Therefore it''s not for test but to populate and I don''t think factories > are good for so many objects, right? > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> put the function in the loop after the user.save ?What exactly do you mean by put it in the loop after .save? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Sep-13 19:56 UTC
Re: Re: Re: after_create not being called in seeds.rb?
sorry now i see it makes no sense, i understood that this is all in your seed.rb and both functions will only be used once and i was suggesting this 100.times do user = User.new user.username = Faker::Name.first_name user.email = user.username+"@example.com" user.save! write_comments(user) <=== this could be in your controller create action end protected def write_comments(user) comment = Comment.new comment.text = "some text" comment.user = user comment.save! end After paying more attention to your comment i notice you want the write_comments to be in your model in production, and my suggestion made no sense. On Mon, Sep 13, 2010 at 3:47 PM, Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > put the function in the loop after the user.save ? > What exactly do you mean by put it in the loop after .save? > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ah, I see. Yeah, after_create seems better after all. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.