7.1.3. Interactive Consoles

简介:
7.1.3. Interactive Consoles 
7.1.3.1. Using Irb 
在rails开发工具中最重要的莫过于交互式命令行了,或者是控制台。使用ruby的标准交互式ruby库(Irb),在控制台中通过命令行你可以访问应用中的每一个部分。你可以加载ActiveRecord对象,检查和编辑数据,往数据库中保存更改的数据。你可以提交控制器请求,并且检查其结果,不单是对HTML响应,而且包括模板分配,session状态,flash等等内容。 

如果你是Irb新手,现在赶快来上手试试吧!在命令提示符下输入irb,运行之后,会出现: 
irb(main):001:0> 
现在输入任意的ruby代码(例如,123+456),按一下回车,Irb会输出结果: 
irb(main):001:0> 123 + 456 
  579 
在Ruby中,要记得,所有一切甚至是整数都是对象,都有方法。每一个对象都有一个叫做class的方法,调用这个方法就会得到对象所属的类型: 
irb(main):002:0> 123.class 
  Fixnum 
另一个普遍适用的方法是methods,调用这个方法会返回定义在此对象上可用的方法数组: 
irb(main):003:0> 123.methods 
=> ["method", "%", "between?", "send", "<<", "prec", "modulo", "&",    
"object_id", ">>", "zero?", "size", "singleton_methods", "__send_    _",    
"equal?", "taint", "id2name", "*", "next", "frozen?",    
"instance_variable_get", "+", "kind_of?", "step", "to_a",    
"instance_eval", "-", "remainder", "prec_i", "nonzero?", "/", "type",    
"protected_methods", "extend", "floor", "to_sym", "|", "eql?",    
"display", "quo", "instance_variable_set", "~", "hash", "is_a?",    
"downto", "to_s", "prec_f", "abs", "singleton_method_added", "class",    
"tainted?", "coerce", "private_methods", "^", "ceil", "untaint", "+@",    
"upto", "-@", "div", "id", "**", "times", "to_i", "<", "inspect",    
"<=>", "==", ">", "===", "succ", "clone", "public_methods", "round",    
">=", "respond_to?", "<=", "freeze", "divmod", "chr", "to_f", "__id_    _",    
"integer?", "=~", "methods", "nil?", "dup", "to_int",    
"instance_variables", "[]", "instance_of?", "truncate"] 

这些例子中是怎么回事?数字(像123这样的整数)一般都不会被认为是对象,但是在ruby中,他们就是对象。这是一个内省的例子:Ruby能够查看对象本身的内容(例如,询问一个对象的类型或者对象支持什么方法)。看一下这个方法数组,你可能注意到有一个方法叫做next,并且想知道它的作用,可以尝试一下: 
irb(main):004:0> 123.next 
  124 
结果很简单:就是在本身的基础上加了一。自省对于探索和学习Ruby和Rails对象是一个很有价值的工具。只要查看一下对象所支持的方法,尝试一下,你就会学习到更多的内容。 
7.1.3.2. Using the Rails console with ActiveRecord 
现在退出Irb(输入quit命令),切换到Rails控制台,其实rails控制台就是一个经过包装的Irb,自动将你所开发应用的整个环境装入其中。可以使用控制台在应用程序的不同层面进行操作,可以直接操作域对象(譬如,ActiveRecord对象,或者模型),或者向控制器提交请求。为了认识一下它是如何在model层面工作,现在假设你有一个简单的数据库结构,由users和articles两张表组成,他们的关系是一对多。对应的模型中定义了他们的关系和数据验证: 
class Article < ActiveRecord::Base 
    belongs_to :user 
    validates_presence_of :title 
end 

class User < ActiveRecord::Base 
    has_many :articles 
    validates_presence_of :name 
end 

在model中声明了这样的模型关系,Rails控制台提供了丰富的环境与域模型进行交互。输入script/console, 
$ script/console 
Loading development environment. 

>> a = Article.new 
=> #<Article:0x22409e8 @attributes={"user"=>nil, "title"=>nil,    
"body"=>nil}, @new_record=true> 

>> a.name = "Using script/console" 
=> "Using script/console" 

>> a.save 
=> true 

>> User.create :name => "Scott" 
=> #<User:0x22289c4 @attributes={"name"=>"Scott", "id"=>1}> 

>> u=User.find :first 
=> #<User:0x223587c @attributes={"name"=>"Scott", "id"=>"1"}> 

>> a.update_attributes :user_id => u.id 
=> true 

>> u.articles 
=> [#<Article:0x222bc64 @attributes={"body"=>nil, "title"=> 
"Using script/console", "id"=>"1", "user_id"=>"1"}>] 

>> u.articles.create 
=> #<Article:0x2223758 @attributes={"body"=>nil, "title"=>nil,    
"user_id"=>1}, @errors=#<ActiveRecord::Errors:0x2222efc, @errors={ 
"title"=>["can't be blank"]}>> 
看看其中发生了什么,首先,我们用new(未保存)方法实例化了一个ActiveRecord对象,这个对象赋给了一个变量a,并且对于a的描述出现在了这句命令的下方(#<…>是ruby通过文本方式描述对象的标准方式)。因为ActiveRecord会自动为每一个数据库字段创建读取和赋值的方法,所以你可以很轻易的改变对象的属性(a.title=’…’),然后保存记录到数据库中(a.save). 

下一行显示了同样的过程,只是将这个过程浓缩成了一句代码,使用create方法。然后,使用update_attributes,我们创建两个记录之间的关系,并且访问了用户(user)的文章(articles)这样一个关系,结果返回的是一个数组,数组内容是Article对象。最后,我们尝试创建一个新的article,但是没有成功,因为初始化的数据验证失败,错误在控制台中显示了出来。 

正如你所看到的,完整的ActiveRecord 接口在Rails控制台中都是可以使用的,所以籍此控制台成为和模型相关问题的一个重要的测试工具。 




本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/115630,如需转载请自行联系原作者

目录
相关文章
|
9月前
|
Shell Linux C++
报错:CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate‘. 问题解决
报错:CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate‘. 问题解决
172 0
|
Shell
【已解决】CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate‘.
【已解决】CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate‘.
308 0
|
Shell
CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate‘.简单解决方案
CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate‘.简单解决方案
1615 0
CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate‘.简单解决方案
|
JavaScript 前端开发
Guidelines for Function Compute Development - Troubleshoot Timeout Issues
Endless codes and endless bugs When you write code, you may inadvertently introduce some hidden bugs, even if you test a large proportion of the codes to the maximum extent possible.
1600 0