# Populate default data in tables when object is created

**URL:** <https://www.ruby-forum.com/t/populate-default-data-in-tables-when-object-is-created/191490>\
**Category:** Rails\
**Created:** [July 13, 2010, 1:45pm UTC](https://www.ruby-forum.com/t/populate-default-data-in-tables-when-object-is-created/191490 "2010-07-13T13:45:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kinunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kinunt](https://www.ruby-forum.com/u/kinunt)\
**Post date:** [July 13, 2010, 1:45pm UTC](https://www.ruby-forum.com/t/populate-default-data-in-tables-when-object-is-created/191490/1 "2010-07-13T13:45:09Z")

</div>

Hi,

How can I populate a table (create several default objects) after a  
company (an object) is created. I would use after\_create inside the  
Company model but I need to populate a lot of data and I think the model  
is not a good place. I’m thinking about having the data I want to  
populate in a yml file and insert it in a table when a company is  
created using after\_create, but I don’t know how to do that. Any hint?  
Thanks!

---

<div class="post-metadata">

**Author:** ![kinunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kinunt](https://www.ruby-forum.com/u/kinunt)\
**Post date:** [July 13, 2010, 3:11pm UTC](https://www.ruby-forum.com/t/populate-default-data-in-tables-when-object-is-created/191490/2 "2010-07-13T15:11:09Z")

</div>

Juan K. wrote:

> I would use after\_create inside the Company model but I need to populate  
> a lot of data and I think the model is not a good place.

Why is that? It certainly sounds like your creation of a company  
requires the writing of some additional data, which are effectively  
default attributes of a company, no?

---

<div class="post-metadata">

**Author:** ![kinunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kinunt](https://www.ruby-forum.com/u/kinunt)\
**Post date:** [July 13, 2010, 3:32pm UTC](https://www.ruby-forum.com/t/populate-default-data-in-tables-when-object-is-created/191490/3 "2010-07-13T15:32:42Z")

</div>

Ar Chron wrote:

> Juan K. wrote:
> 
> > I would use after\_create inside the Company model but I need to populate  
> > a lot of data and I think the model is not a good place.
> 
> Why is that? It certainly sounds like your creation of a company  
> requires the writing of some additional data, which are effectively  
> default attributes of a company, no?

For example, a Company has\_many Threats and a Threat belongs\_to a  
Company and I want to populate the table Threats with some default  
threats when a Company is created. I don’t use the same threats for each  
company because each company can add, delete and modify their threats as  
they want, so I don’t want to share threats. If you think this can be  
done differently better any hint will be really appreciated 🙂

---

<div class="post-metadata">

**Author:** ![kinunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kinunt](https://www.ruby-forum.com/u/kinunt)\
**Post date:** [July 14, 2010, 6:47am UTC](https://www.ruby-forum.com/t/populate-default-data-in-tables-when-object-is-created/191490/4 "2010-07-14T06:47:51Z")

</div>

Juan K. wrote:

> For example, a Company has\_many Threats and a Threat belongs\_to a  
> Company and I want to populate the table Threats with some default  
> threats when a Company is created.

Precisely! The default action for a ‘company creation’ step is to  
manufacture n threats associated with that company. The behavior of  
defaulting those records for each company as it is created belongs in  
the Company model (in my opinion).  
If you have different ‘default’ threats depending on what kind of  
company is created (threats are dependent on a company attribute), then  
all the more reason for that defaulting logic to be contained in the  
company model.

> I don’t use the same threats for each  
> company because each company can add, delete and modify their threats as  
> they want, so I don’t want to share threats. If you think this can be  
> done differently better any hint will be really appreciated 🙂

You would create an instance of each desired threat type for each  
company when the company was created. No threat sharing was intended in  
my earlier response, sorry for the ambiguity.

---

<div class="post-metadata">

**Author:** ![kinunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kinunt](https://www.ruby-forum.com/u/kinunt)\
**Post date:** [July 13, 2010, 3:36pm UTC](https://www.ruby-forum.com/t/populate-default-data-in-tables-when-object-is-created/191490/5 "2010-07-13T15:36:26Z")

</div>

Juan K. wrote:

> Hi,
> 
> How can I populate a table (create several default objects) after a  
> company (an object) is created. I would use after\_create inside the  
> Company model but I need to populate a lot of data and I think the model  
> is not a good place.

And I think you’re wrong. 🙂 The model is the right place for logic  
that has to do with model objects!

> I’m thinking about having the data I want to  
> populate in a yml file and insert it in a table when a company is  
> created using after\_create, but I don’t know how to do that.

You’d do it the same way as the first method; just read the YAML file  
instead of putting the data in the code.

> Any hint?  
> Thanks!

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
