Rails脚手架特定表的奇怪的未定义方法错误

时间:2013-04-25 12:11:53

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

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

以下是使用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'在某种程度上是由铁路用于i18n的东西,但是在命名模型'Bla'时会发生同样的问题。

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

所以问题是名为class的列,红宝石显然很讨厌。

此StackOverflow问题中发布的解决方案:Legacy table with column named "class" in Rails

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

相关问题