Filling the database after "Registration"

I would like to populate various tables in my database after a new customer has signed up to use our web application. I am wondering that the best way to do this is by the main requirement that it does not require intervention on my part. Most of the data is static (but can be changed by the client later (eg preferences)), but obviously the client ID will be required as a way to link the generated records to that client.

I thought about putting some

Object.create(:customer_id => @customer.id, :yummy => "maybe", :etc => true)

in the controller that handles the registration, but that annoying little call that tells me the best way is gone again!

Thanks in advance for any suggestions!

Woof

+2


source to share


6 answers


In your controller

class AccountController < ApplicationController
after_filter :populate_db  :only=>[:register] 
  def populate_db
    # whatever stuff
    Account.populate
  end
end

      



And put the logic inside your model:

class Account < ActiveModel::Base
  def populate
  # your logic
  end
end

      

+2


source


The problem with khelll's solution is that if you create a new account using out of case (such as in the admin module) the database will not be populated.

So, I would prefer something like the following:



class Account < ActiveModel::Base
    def after_create
        populate
    end

    private
    def populate
        # Your logic
    end
end

      

After the account is created, the after_create callback will be called.
It's more MVC and DRY compatible;)

+3


source


two ways

  • a trigger function in your database that does this
  • callback in your user model on creation
0


source


Can't you just set the defaults in your database?

0


source


I am using a YAML file to populate more than ten tables in the database during registration. This approach has some positive side effects for me:

  • I can easily change the contents of the seed data for the new account
  • It's very easy to add localized versions of source content, I can easily translate YAML files via 99translations.com
  • I plan on adding a data import feature later when new customers can import their data during or immediately after registration, so they can just take the import file template and customize it to their needs.

There were also some disadvantages:

  • Inevitably, when the data schema changes, sometimes I have to change all the localized versions of those files.
  • This is one piece of ugly code that contains foreign keys between all new records. Maybe I can use some schema descriptors here ...

When I get about 300 new registrations in one day, it's too early for me to worry about performance.

0


source


@Omar Qureshi You shouldn't be using a trigger function in your database - there are many possibilities that can be done by them, but ActiveRecord is database agnostic and has callbacks that handle things like triggers can fire. Using something that ties you to a specific DB system is wrong.

@ dmathieu's answer seems to be the best. And you should consider using Factory girl - it's a great tool to populate your database.

-1


source







All Articles