ALi
ALi

Reputation: 428

how set default value of db in ruby on rails

i was wondering to set default value when we type command for model, i.e

rails g model admin user:string password:string

how i can set default value to user and pass "admin"

something like.

rails g model admin user:string default:"admin" password:string default:"admin"

Thanks

Upvotes: 2

Views: 2816

Answers (2)

VvDPzZ
VvDPzZ

Reputation: 3095

In your migration file, e.g.

t.decimal :bounties, :precision => 8, :scale => 2, :default => 0

OR set a callback

before_create :set_admin

def set_admin
  self.role = "admin"
end

Upvotes: 4

Amar
Amar

Reputation: 6942

Hi one way is in migration file set default value by providing default hash and then migrate. or you can override new methods or if you want to create one admin user for application you can put into seed data.

Upvotes: 0

Related Questions