2013-07-04 27 views
1

因此,我有一个为旧的php应用程序设置的db,我无法触及数据库的完成方式。Rails中列名的别名属性

不过,我在db一些列名称使用的是一些受限制的话用Ruby像“阶级”

因此使其工作,我用alias_attribute

但它不工作。下面是代码:

class Contractor < ActiveRecord::Base 
    set_table_name "contractors" 
    alias_attribute :class_type, :class 
    attr_accessible :about, :alias, :business_entity, :business_logo, :business_name, :city, :class, :county, :created, :email, :founded, :metroarea, :nid, :phone, :state, :status, :street_address, :uid, :zipcode 
end 

这里的错误:

1.9.3-p125 :001 > Contractor.last 
SyntaxError: /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:345: syntax error, unexpected ')', expecting '=' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:343:in `module_eval' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:343:in `define_optimized_call' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:227:in `block in alias_attribute' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:224:in `each' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:224:in `alias_attribute' 
    from /Users/snoopy/Projects/buildzoom/ruby/service_request/app/models/contractor.rb:3:in `<class:Contractor>' 
    from /Users/snoopy/Projects/buildzoom/ruby/service_request/app/models/contractor.rb:1:in `<top (required)>' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `load' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `block in load_file' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:639:in `new_constants_in' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:468:in `load_file' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:353:in `require_or_load' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:502:in `load_missing_constant' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:192:in `block in const_missing' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `each' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `const_missing' 
    from (irb):1 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start' 
    from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>' 
    from script/rails:6:in `require' 
    from script/rails:6:in `<main>' 

1.9.3-p125 :003 > Contractor.where(:zipcode => "94111") 
    Contractor Load (648.6ms) SELECT `contractors`.* FROM `contractors` WHERE `contractors`.`zipcode` = '94111' 
(Object doesn't support #inspect) 
=> 

当我否则删除alias_attribute这是相同的错误第一个最后一个只出现

回答

0

因此之一,有宝石safe_attributes谁正在做这项工作。 到目前为止,它工作得很完美。

0

我认为你的问题是你给的别名:“class” class是的ActiveRecord的方法 您可以通过运行该命令

Contractor.first.methods 

定义这个别名只是以某种方式“越权”它看到它,并中断对轨操作