簡體   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