# Automatically creating associated records with after\_create

**URL:** <https://www.ruby-forum.com/t/automatically-creating-associated-records-with-after-create/65278>\
**Category:** Rails\
**Created:** [August 17, 2006, 8:13am UTC](https://www.ruby-forum.com/t/automatically-creating-associated-records-with-after-create/65278 "2006-08-17T08:13:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [August 17, 2006, 8:13am UTC](https://www.ruby-forum.com/t/automatically-creating-associated-records-with-after-create/65278/1 "2006-08-17T08:13:45Z")

</div>

Hi all.

I am actually using ActiveRecord outside of Rails but I don’t see that  
it matters for this question.

I want to automatically create a registration entry when I add a new  
user to my application. This works fine, but it’s not automatic:

class User \< ActiveRecord :: Base  
has\_many :registrations  
end

class Test  
user = User.new  
user.registrations.create  
end

I’d like the User class to do that automatically. I tried this but it  
didn’t work:

class User \< ActiveRecord :: Base  
has\_many :registrations  
after\_create {  
self.registrations.create  
}  
end

I feel like I’m just missing some syntax. Can anyone help?

Thanks!

/adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [August 17, 2006, 9:43am UTC](https://www.ruby-forum.com/t/automatically-creating-associated-records-with-after-create/65278/2 "2006-08-17T09:43:48Z")

</div>

Adam B. \<adam@…\> writes:

> class User \< ActiveRecord :: Base  
> has\_many :registrations  
> after\_create {  
> self.registrations.create  
> }  
> end

self.registrations returns a list of Registration objects, and that list  
(I  
assume it’s just an array) doesn’t have a .create method

self.registrations \<\< Registration.new # should work though

The above should be read as “Append a new registration to  
self.registrations”

Gareth

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [August 18, 2006, 12:54am UTC](https://www.ruby-forum.com/t/automatically-creating-associated-records-with-after-create/65278/3 "2006-08-18T00:54:49Z")

</div>

Thanks for the reply.

But I’m confused: if that’s the case, how come user.registrations.create  
works?

Anyway, I tried adding this line to the User class:

after\_create { self.registrations \<\< Registration.new(({:timestamp =\>  
Time.now})) }

And I got this error:

…/gems/activerecord-1.14.4/lib/active\_record/base.rb:1129:in  
`method_missing': undefined method`registrations’ for User:Class  
(NoMethodError)

Also, my Registration class looks like:

class Registration \< ActiveRecord::Base  
belongs\_to :users  
end

I appreciate the assistance!

/afb

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [August 18, 2006, 7:15am UTC](https://www.ruby-forum.com/t/automatically-creating-associated-records-with-after-create/65278/4 "2006-08-18T07:15:39Z")

</div>

Adam B. wrote:

> Also, my Registration class looks like:
> 
> class Registration \< ActiveRecord::Base  
> belongs\_to :users  
> end

You might try using ‘belongs\_to :user’. The ActiveRecord parser is  
exacting in the names used in one-to-many models.

Jose

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [August 18, 2006, 8:13am UTC](https://www.ruby-forum.com/t/automatically-creating-associated-records-with-after-create/65278/5 "2006-08-18T08:13:34Z")

</div>

Jose, you are right and I fixed that but was still having the same  
problem.

Then I changed my User class to:

class User \< ActiveRecord :: Base  
has\_many :registrations  
after\_create :create\_new\_registration

private  
def create\_new\_registration  
self.registrations \<\< Registration.new  
end  
end

And that worked. Thanks all for your suggestions.

/afb
