ruby on rails
I'm working on a Ruby 1.8 / Rails 2.1 application which is connecting to a SQL Server 2008 database.
When I try to use a find_by_CompanyCode
method, ActiveRecord returns a NoMethodError. Yet, from the following you can see that the table exists and has the method in question.
Not sure what I'm missing.. any help would be appreciated
EDIT: Only fields which end in "Code" aren't appearing when I just run IvantageEmployee.first .. error occurs from a view. Moving the exact same code to a controller, the code works as expected.
eval @goal.organization_type.employee_class + ".find_by_#{@goal.organization_type.employee_field_code}('#{@goal.specifier}').#{@goal.organization_type.employee_field_description}"
>> IvantageEmployee.first.CompanyCode => "GAI" >> IvantageEmployee.find_by_CompanyCode('GAI') NoMethodError: undefined method `find_by_CompanyCode' for IvantageEmployee(Table doesn't exist):Class from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1613:in `method_missing_without_paginate' from /usr/lib/ruby/gems/1.8/gems/will_paginate-2.3.15/lib/will_paginate/finder.rb:170:in `method_missing' from (irb):7 >>
Some more information. The table has several fields. The one I'm struggling with is CompanyCode, but it also has RegionDescription.
Note the following console output. find_by_RegionDescription works; find_by_CompanyCode doesn't work. Also, CompanyCode doesn't appear when I just output the class, but RegionDescription doesn't Not sure why ActiveRecord would be missing fields that are on the table
>> IvantageEmployee.find_by_RegionDescription 'GAI' ### Finding method find_by_RegionDescription => nil >> IvantageEmployee.find_by_CompanyCode 'GAI' ### Finding method find_by_CompanyCode NoMethodError: undefined method `find_by_CompanyCode' for IvantageEmployee(Table doesn't exist):Class from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1614:in `method_missing_without_paginate' from /usr/lib/ruby/gems/1.8/gems/will_paginate-2.3.15/lib/will_paginate/finder.rb:170:in `method_missing' from (irb):9 >>
You need to use snake case instead of camel case:
IvantageEmployee.find_by_company_code('GAI')
The convention in ruby is to use camel case for classes and snake case for methods. Rails follows that convention(as should you) so you can assume that any methods dynamically created by it are going to be snake case.
链接地址: http://www.djcxy.com/p/49730.html下一篇: 红宝石在轨道上