After_create not being called in seeds.rb?

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?

i think self is a reserved word! shouldnt it be current_user?

Heinz S. 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 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
[email protected]

Please quote when replying; otherwise, it gets difficult to follow the
discussion.

Heinz S. 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 populate

So 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!
end

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.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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?

@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?

On Sunday 12 September 2010, Heinz S. wrote:

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 S.
mailto:[email protected]
http://www.schuerig.de/michael/

For some stupid reason it’s working now. My guess is a Windows reboot
did the magic :wink: It is indeed much nicher!

Thanks everybody!

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?

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.

put the function in the loop after the user.save ?
What exactly do you mean by put it in the loop after .save?

Ah, I see. Yeah, after_create seems better after all.

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