Rails Load Data into Application every time a New User is Created

345 views Asked by At

I am trying to figure out the "right" way to do this.

In my application every time I create a new user I want to have specific data loaded into an associated table. The associated table is a different preset lists. So a User has many lists and those lists have many items. Some of those Items I want to be loaded when the User is created.

I really don't know how to go about doing this but I am guessing something like: create User after_create: create Lists (need already existing data for the list names) after_create List then populate Items table with existing data for these Lists.

Should I be using seed data for this? Is that alright for production and if so how exactly would I go about doing that.

Any guidance or other recommendations are greatly appreciated.

2

There are 2 answers

0
shingara On

The using of seed is really made for insert all data needed by your application like your list.

For me it's the good way. I don't have some pointer. By example, In redmine there are a bootstrap insertion.

0
Toby Hede On

Seed is really for inserting data on the first deployment of the application.

If you are creating records attached to specific events, then your approach is probably the best way to go. If there is a LOT of data, you can externalize it into YAML or something along those lines, but the after_create works fine for me in several situations.

class User < ActiveRecord

  def after_create
     List.create
  end
end

One handy trick that sometimes works well is to have a canonical user in the system - load this data and clone it for you current user.