jojo
jojo

Reputation: 13843

how to maintain model properties

i am new to ROR, just setup the enviroment ruby 1.9.2 with rails 3.0.6

just notice one thing. after create model.. propeties has been add into database but couldn`t see from the class.. and it just work for me.

looks like rails will scan my database schema before launching the application..

but i have a question for it:

if i frequently add or remove columns... how do i know what properties do i have if i do not define in the class? can i manually add properties into ActiceRecord class(es) ??

class User < ActiveRecord::Base
  has_one :contact
  validates_confirmation_of :password
end



class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :username
      t.string :password

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

Upvotes: 0

Views: 92

Answers (2)

Kevin
Kevin

Reputation: 1569

i;m not sure i understand the question, but you can just view the schema.rb

any migration that you make will update the schema

Upvotes: 1

ceth
ceth

Reputation: 45295

You can look at db/schema.rb file to see database schema you have.

And yes, sometimes I add properies to the model class, but they are virtual properties:

def full_name
   [first_name, last_name].joun(' ')
end

def full_name=(name)
    split = name.split(' ', 2)
    self.first_name = split.first
    self.last_name = split.last
end  

In view:

<p> Full name </p>  
<%= f.text_field :full_name %>

Upvotes: 1

Related Questions