使用 find_by_<field> 返回错误"Table doesn't exist"



我正在开发一个Ruby 1.8/Rrails 2.1应用程序,该应用程序正在连接到SQL Server 2008数据库。

当我尝试使用find_by_CompanyCode方法时,ActiveRecord会返回一个NoMethodError。然而,从下面的内容中可以看出,该表是存在的,并且有问题的方法。

不确定我错过了什么。。如有任何帮助,将不胜感激

编辑:当我刚运行IvantageEmployee.first时,只有以"代码"结尾的字段不会出现。。视图中出现错误。将完全相同的代码移动到控制器,代码将按预期工作。

eval@goal.orgorganization_type.employe_class+".find_by_#{@goal.organization_type.memployee_field_code}('#{@goal.specifier}').#{@goal.Organizationtype.memployae_field_description}">
>IvantageEmployee.first.CompanyCode=>"GAI">>IvantageEmployee.find_by_CompanyCode('GAI')NoMethodError:IvantageEmployee的未定义方法"find_by_CompanyCode"(表不存在):类来自/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1613:在`method_missing_without_paginate'中来自/usr/lib/ruby/gems/1.8/gems/will_paginate-2.3.15/lib/will_pagenate/finder.rb:170:in `method_missing'来自(irb):7>>

更多信息。该表有几个字段。我正在努力解决的是CompanyCode,但它也有RegionDescription。

注意以下控制台输出。find_by_RegionDescription有效;find_by_CompanyCode不起作用。此外,当我刚输出类时,CompanyCode不会出现,但RegionDescription不确定为什么ActiveRecord会缺少表上的字段

>IvantageEmployee.find_by_RegionDescription'GAI'###查找方法find_by_RegionDescription=>零>>IvantageEmployee.find_by_CompanyCode'GAI'###查找方法find_by_CompanyCodeNoMethodError:IvantageEmployee的未定义方法"find_by_CompanyCode"(表不存在):类来自/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1614:在`method_missing_without_paginate'中来自/usr/lib/ruby/gems/1.8/gems/will_paginate-2.3.15/lib/will_pagenate/finder.rb:170:in `method_missing'来自(irb):9>>

您需要使用snake case而不是camel case:

IvantageEmployee.find_by_company_code('GAI')

ruby中的约定是对类使用camel大小写,对方法使用snake大小写。Rails遵循该约定(您应该如此),因此您可以假设它动态创建的任何方法都将是snake情况。

相关内容

最新更新