天天看点

Rails Cookbook翻译(五)

(本翻译纯粹为了学习,切勿用于商业目的,转载请指明出处:http://blog.csdn.net/fuliangliang)

Rails Cookbook翻译(五)

Recipe 3.5 在Rails控制台上观察模型关系

问题:

   你想查看你的模型对象之间的关系以便确认你已经正确进行了设置。你可以启动web程序来这么做,但你想把事情做得简单点,那何妨劳驾一下命令行呢。

解决方案:

   使用Rails控制台创建模型对象,并且建立它们之间的关系。

在你的工程根目录下,输入:

 ~/projects $ ruby script/console –s

Loading development environment in sandbox.

Any modification you make will be rooled back on exit.

你可以在windows下,使用:

C:/myApp>ruby script/console -s
        

你接着进入了irb会话,可以完全访问你的工作环境和Active Record模型了。你可以输入Ruby代码,就像在控制器里一样,这样你可以发现你的数据模型中的任何问题了。

讨论:

作为一个演示,我们为工程创建了一个数据库,用它来跟踪assets和它的types。这个例子也与assets和tags关联。通过使用script/generate:asset,asset_type,以及tag产生的三个模型来创建数据库(注意:你不想为assets_type连接表创建一个模型,因为Rails内部会处理这些)。

~/project$ ruby script/generate model asset
        
...
        
~/project$ ruby script/generate model asset_type 
        
...
        
~/project$ ruby script/generate model tag
        
...
        

现在,我们使用了下面的migration来定义表:

Class BuildDb < ActiveRecord::Migration

def self.up

  create_table :asset_types do |t|

t.column :name, :string

  end

  create_table :assets do |t|

     t.column :asset_type_id, :integer

     t.column :name   :string

     t.column :decription :text

  end

  create_table :assets_tags do |t|

  t.column :name, :string

end

create_table :assets_tags do |t|

  t.column :asset_id, :integer

  t.column :tag_id , :integer

end

end

def self.down

  drop_table :assets_tags

  drop_table :assets

  drop_table :asset_types

  drop_table :tags

end

end

现在你可以使用一些哑数据来操作数据库了。使用下面的insert语句:

 insert into asset_types values (1,'Photo');
        
insert into asset_types values (2,'Painting');
        
insert into asset_types values (3,'Print');
        
insert into asset_types values (4,'Drawing');
        
insert into asset_types values (5,'Movie');
        
insert into asset_types values (6,'CD');
        
insert into assets values (1,1,'
  
   
    Cypress
   
  ','A photo of a tree.');
        
insert into assets values (2,5,'Blunder','An action film.');
        
insert into assets values (3,6,'Snap','A recording of a fire.');
        
insert into tags values (1,'hot');
        
insert into tags values (2,'red');
        
insert into tags values (3,'boring');
        
insert into tags values (4,'tree');
        
insert into tags values (5,'organic');
        
insert into assets_tags values (1,4);
        
insert into assets_tags values (1,5);
        
insert into assets_tags values (2,3);
        
insert into assets_tags values (3,1);
        
insert into assets_tags values (3,2);
        

现在设置这些模型之间的关系。这个例子包含了一对一和一对多的关系。

asset_type.rb

class AssertType < ActiveRecord::Base

 has_many :assets

end

tag.rb

class Tag < ActiveRecord::Base

 has_and_belongs_to_many :assets

end

assert.rb

class Assert < ActiveRecord::Base

 belongs_to :asset_type

 has_and_belongs_to_many :tags

end

既然我们已经设置好了模型,加载了一些数据,我们可以打开一个控制台会话来看看了:

~/project$ ruby script/console -s
        
Loading development environment in sandbox.
        
Any modifications you make will be rolled back on exit.
        
>> a = Asset.find(3)
        
=> #<Asset:0x4093fba8 @attributes={"name"=>8220;Snap", "id"=>"3",
        
"asset_type_id"=>"6", "description"=>"A recording of a fire."}>
        
>> a.name  
        
=> "Snap" 
        
>> a.description
        
=> "A recording of a fire." 
        
>> a.asset_type 
        
=> #<AssetType:0x4093a090 @attributes={"name"=>"CD", "id"=>"6"}>
        
>> a.asset_type.name
        
=> "CD" 
        
>> a.tags
        
=> [#<Tag:0x40935acc @attributes={"name"=>"hot", "tag_id"=>"1", "id"=>"1",
        
"asset_id"=>"3"}>, #<Tag:0x40935a90 @attributes={"name"=>"red", "tag_id"=>"2",
        
"id"=>"2", "asset_id"=>"3"}>]
        
>> a.tags.each { |t| puts t.name }
        
hot
        
red
        
=> [#<Tag:0x40935acc @attributes={"name"=>"hot", "tag_id"=>"1", "id"=>"1",
        
"asset_id"=>"3"}>, #<Tag:0x40935a90 @attributes={"name"=>"red", "tag_id"=>"2",
        
"id"=>"2", "asset_id"=>"3"}>]
        

在这个控制台会话中,我们使用id为3的参数获取了asset纪录,并且将它存到一个对象中。我们显示了对象的名字和描述。访问asset的类型,返回了一个AssetType对象。下一行返回了asset type的名字。

访问asset对象的tags,返回了一个包含asset的tags对象数组。下一个命令跌代的访问这些tags并且返回了tag的名字。

当一个对象变得更大更复杂时,在控制台上打印出来的信息变得非常难读。通过使用pp(pretty-print)或者y(yaml)来打印模型,可以很大程度的改善信息的可读性。在控制台上使用下面的命令:

  require 'pp'
        
asset = Asset.find(:first)
        
pp asset
        
y asset      

y方法以YAML格式打印,使用:

 puts asset.to_yaml  

真是是一个捷径。

在一个裸露的环境里测试你的模型是确定你的模型没有问题的很好的方法。在你应用程序的控制器里做类似的测试会使得一个很明显的问题变得很难发现。

你还可以参考:

10.1章