Gav
Gav

Reputation: 11460

Populating a database after "Sign Up"

I would like to populate various tables in my database after a new customer signs up to use our web application. I'm wondering what the best way to do this is, the main requirement being that it should not require any intervention by me. Most of the data is static (but can be changed by the customer later (like preferences for example)), but will obviously need the customer's ID as a way of linking the created records to this customer.

I considered putting a few

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

statements in the controller that handles signups, but that annoying little alarm bell that tells me there's a better way is going off again!

Thanks in advance for any suggestions!

Gav

Upvotes: 2

Views: 298

Answers (6)

Priit
Priit

Reputation: 5248

I'm using a YAML file to populate more than ten tables in database during signup. This approach has some positive side-effects for me:

  1. I can easily change the contents of the seed data for new account
  2. It is very easy to add localized versions of initial content, i can let YAML files translated easily through 99translations.com
  3. I'm planning to add data import feature later on where new clients can import their data during or right after signup so they can just take the import file template and customize it for their own needs.

There have been some downsides too:

  1. Inevitably, when data schema changes, sometimes i have to modify all localized versions of these files.
  2. It is one piece of ugly code i have that maps the foreign keys between all the new records. Maybe i can use some schema descriptors here...

As i get around 300 new signups in a day, it's too early for me to worry about performance.

Upvotes: 0

Bartosz Pietrzak
Bartosz Pietrzak

Reputation: 146

@Omar Qureshi You shouldn't use trigger function in your database - there are many features that can be done by them, but ActiveRecord is database-agnostic and it has callbacks that handle such things that triggers can. Using something that links 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 for populating the DB.

Upvotes: -1

Gareth
Gareth

Reputation: 138110

Can you not just set defaults in your database?

Upvotes: 0

Damien MATHIEU
Damien MATHIEU

Reputation: 32627

The problem with khelll's solution is that if you create a new Account using from outside of the register action (for example, in an admin module), it's database won't be populated.

So I'd prefer something like the following :

class Account < ActiveModel::Base
    def after_create
        populate
    end

    private
    def populate
        # Your logic
    end
end

The after_create callback will be called after any account creation.
That's more MVC and DRY compliant ;)

Upvotes: 3

khelll
khelll

Reputation: 24010

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

Upvotes: 2

Omar Qureshi
Omar Qureshi

Reputation: 9093

two ways

  1. a trigger function in your database that does this
  2. a callback in your user model on creation

Upvotes: 0

Related Questions