繁体   English   中英

Rails脚手架特定表的奇怪的undefined_method错误

[英]Rails scaffold strange undefined_method error for specific table

我正在为现有数据库开发一个管理工具,并在搭建特定表时遇到一个奇怪的问题。

以下是使用rake db:schema:dump的表的rake db:schema:dump

create_table "locality", :force => true do |t|
    t.integer "version",             :limit => 8,                  :null => false
    t.string  "truth_id",                                          :null => false
    t.string  "truth_record_id",                                   :null => false
    t.binary  "deleted",             :limit => 1,                  :null => false
    t.string  "internal_code",                                     :null => false
    t.string  "iso_code"
    t.string  "materialized_path",                                 :null => false
    t.string  "invariant_name",                                    :null => false
    t.binary  "published",           :limit => 1,                  :null => false
    t.float   "geo_point_latitude",               :default => 0.0
    t.float   "geo_point_longitude",              :default => 0.0
    t.string  "class",                                             :null => false
    t.integer "hut_count",                        :default => 0
    t.integer "hotel_count",                      :default => 0
    t.string  "destination_url"
  end

 add_index "locality", ["truth_record_id"], :name => "truth_record_id", :unique => true

我使用schema_to_scaffold gem从转储模式创建我的脚手架:

rails g scaffold locality version:integer truth_id:string truth_record_id:string
  deleted:binary internal_code:string iso_code:string materialized_path:string 
  invariant_name:string published:binary geo_point_latitude:float 
  geo_point_longitude:float class:string hut_count:integer hotel_count:integer 
  destination_url:string

这个工作流程适用于很多其他表,但是当在rails控制台中访问/ localities或Locality.all时,我得到它:

irb(main):001:0> Locality.all
Locality Load (2.1ms)  SELECT `locality`.* FROM `locality` 
NoMethodError: undefined method `attribute_method_matcher' for "Country":String

“国家”在哪里:字符串来自哪里? 起初我认为模型名称'locality'在某种程度上是由rails用于i18n的东西,但在命名模型'Bla'时会出现同样的问题。

我正在使用rails 3.2.13和MySQL数据库。

我相信你的专栏:课程无效。 您如何访问该列,因为该类已经是ruby中任何对象的方法?

我认为这会造成混乱。 加载位置的类列的值是“国家”吗?

所以问题是列名为class ,ruby显然很讨厌。

此StackOverflow问题中发布的解决方案: Rails中名为“class”的列的旧表

或者更具体地说,在这篇博客文章中(访问日期:2013年3月25日): http//kconrails.com/2011/01/28/legacy-database-table-column-names-in-ruby-on-rails-3/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM