Rails之Model

首先安装ruby,rails,gem等工具

ssy@ubuntu:~/test/demoproject$ rails -v
Rails 2.3.14
ssy@ubuntu:~/test/demoproject$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
ssy@ubuntu:~/test/demoproject$ gem -v
1.8.23

sudo gem install sqlite3 -v '1.3.7'

如果出现如下错误,安装execjs,therubyracer两个库即可

root$ ~/public_html/demo> rails server
/usr/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
 
sudo gem install execjs
sudo gem install therubyracer

vi demo/Geminfo 添加
gem 'execjs'
gem 'therubyracer'
首先创建rails项目,并创建model
rails demoproject
cd demoproject
root$ ruby script/generate model t_model
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/t_model.rb
      create  test/unit/t_model_test.rb
      create  test/fixtures/t_models.yml
      create  db/migrate
      create  db/migrate/20130504114544_create_t_models.rb
如上所示,rails自动生成model文件以及测试文件等

编辑create_t_models.rb如下,定义表结构:

class CreateTModels < ActiveRecord::Migration
  def self.up
    create_table :t_models do |t|
      t.string  :name
      t.string  :passwd, :limit => 40
      t.integer :age
      t.text    :profile, :text
      t.boolean :admin, :default => false

      t.timestamps
    end
  end

  def self.down
    drop_table :t_models
  end
end
运行rake db:migrate生成数据表,如果想增删改表结构,可以实现如下
root$ script/generate migration add_tel_colum
      exists  db/migrate
      create  db/migrate/20130504130846_add_tel_column.rb

class AddTelColumn < ActiveRecord::Migration
  def self.up
    add_column :t_models, :tel, :string
  end

  def self.down
    remove_column :t_models, :tel
  end
end
tails提供console工具实现ORM操作数据表 (script/console production|test)
root$ script/console
>>TModel.create(:name=>"ciaos", :passwd=>"passwd", :age=>25, :profile=>"Find the Next", :admin=>false)
>>TModel.create(:name=>"ciaos2", :passwd=>"passwd2", :age=>26, :profile=>"Find the Next", :admin=>false)

>> t=TModel.new
=> #<TModel id: nil, name: nil, passwd: nil, age: nil, profile: nil, text: nil, admin: false, created_at: nil, updated_at: nil>
>> t.name="ciaos3"
=> "ciaos3"
>> t.passwd="passwd3"
=> "passwd3"
>> t.save
=> true

>>TModel.find_by_age(25)
rails提供development/test/production三种数据库,如果要生成别的数据库,使用如下命令

rake db:setup RAILS_ENV="production"

rake db:create RAILS_ENV=test

./script/server -e production(指定运行数据库)

controller访问model方式如下所示

root$ cat app/controllers/test_controller.rb 
class TestController < ApplicationController
  def index
    @ms = TModel.find(params[:id])
  end
end

 view显示查询结果

root$ cat app/views/test/index.html.erb 
<h1>Test#index</h1>
<p>Find me in app/views/test/index.html.erb</p>
<%= debug(@ms) %>

 启动服务器,请求测试如下

root$ curl http://127.0.0.1:3000/test/index/1
<h1>Test#index</h1>
<p>Find me in app/views/test/index.html.erb</p>
<pre class='debug_dump'>--- !ruby/object:TModel
attributes:
&nbsp; id: 1
&nbsp; name: ciaos1
&nbsp; passwd: passwd
&nbsp; age: 25
&nbsp; profile: Find the Next
&nbsp; text: 
&nbsp; admin: f
&nbsp; created_at: '2013-05-04 12:27:48'
&nbsp; updated_at: '2013-05-04 12:50:05'
&nbsp; tel: 
attributes_cache: {}
</pre>
  数据表一对多关联示例如下
rails generate model Hotel name:string address:string
rails generate model Picture hotel_id:integer url:string
D:\website\demo1>rake db:migrate
 编辑model的association如下
class Hotel < ActiveRecord::Base
  attr_accessible :address, :name
  
  has_many :pictures
end

class HotelPictures < ActiveRecord::Base
  attr_accessible :hotel_id, :url
  
  belongs_to :hotel
end
 使用
> h=Hotel.create(:name=>"Zhong Huang",:address=>"Beijing")
> h
=> #<Hotel id: 1, name: "Zhong Huang", address: "Beijing", created_at: "2013-07-05 03:35:42", updated_at: "2013-07-05 03:35:42">
> p=Picture.create(:hotel_id=>1,:url=>"a.jpg")
> h.pictures
=> [#<Picture id: 1, hotel_id: 1, url: "a.jpg", created_at: "2013-07-05 03:36:16", updated_at: "2013-07-05 03:36:16">]

猜你喜欢

转载自ciaos.iteye.com/blog/1857729