Populate default data in tables when object is created

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!

Juan Kinunt 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?

Ar Chron wrote:

Juan Kinunt 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 :slight_smile:

Juan Kinunt 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. :slight_smile: 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,

Juan Kinunt 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 :slight_smile:

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.