rails4 activeRecord错误

时间:2014-01-14 04:02:26

标签: ruby-on-rails-3 activerecord ruby-on-rails-4

SyntaxError: C:/Users/NEBELYN/Desktop/cms/simple_cms/app/models/subject.r    
b:3: syntax error, unexpected =>, expecting ')'                          
    scope :visible, lambda { where (:visible => true) }              
                                               ^                     
C:/Users/NEBELYN/Desktop/cms/simple_cms/app/models/subject.rb:4: syntax e
rror, unexpected =>, expecting ')'                                       
    scope :invisible, lambda { where (:visible => false) }           
                                                 ^                   
C:/Users/NEBELYN/Desktop/cms/simple_cms/app/models/subject.rb:10: syntax 
error, unexpected keyword_end, expecting '}'                             
    from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/
active_support/dependencies.rb:424:in `load'                             
    from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/
active_support/dependencies.rb:424:in `block in load_file'               
    from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/
active_support/dependencies.rb:616:in `new_constants_in'                 

当我的subject.rb文件看起来像这样

 class Subject < ActiveRecord::Base

scope :visible, lambda { where (:visible => true) }
scope :invisible, lambda { where (:visible => false) }
scope :sorted, lambda {order("subjects.position ASC")}
scope :newest_first, lambda{ order("subjects.created_at DESC")}
scope :search. lambda { |query|
    where (["name like ?", "%#{query}%"])
}

端 当我在rails控制台上运行此命令时“Subject.visible”请帮助

2 个答案:

答案 0 :(得分:1)

您不能在where(之间添加空格。

scope :visible, lambda { where (:visible => true) }   # bad
scope :visible, lambda { where(:visible => true) }    # good

答案 1 :(得分:0)

删除whereopen paranthesis

之间的空格
class Subject < ActiveRecord::Base

scope :visible, lambda { where(:visible => true) }
scope :invisible, lambda { where(:visible => false) }
scope :sorted, lambda {order("subjects.position ASC")}
scope :newest_first, lambda{ order("subjects.created_at DESC")}
scope :search. lambda { |query|
    where(["name like ?", "%#{query}%"])
}