简体   繁体   English

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

[英]Rails scaffold strange undefined_method error for specific table

I'm currently working on an admin tool for an existing database and encountered a strange problem when scaffolding a particular table. 我正在为现有数据库开发一个管理工具,并在搭建特定表时遇到一个奇怪的问题。

Here is the schema of the table using rake db:schema:dump : 以下是使用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

I used the schema_to_scaffold gem to create my scaffold from the dumped schema: 我使用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

This workflow worked for a lot of other tables but when accessing /localities or Locality.all in the rails console all i get its: 这个工作流程适用于很多其他表,但是当在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

Where does "Country":String come from? “国家”在哪里:字符串来自哪里? At first I thought the model name 'locality' is somehow reservers by rails for i18n stuff but the same problem happens when naming the model 'Bla'. 起初我认为模型名称'locality'在某种程度上是由rails用于i18n的东西,但在命名模型'Bla'时会出现同样的问题。

I'm using rails 3.2.13 and a MySQL Database. 我正在使用rails 3.2.13和MySQL数据库。

I believe that your column: class is invalid. 我相信你的专栏:课程无效。 How would you have access to that column since the class is already a method of any object in ruby? 您如何访问该列,因为该类已经是ruby中任何对象的方法?

I think that this causes the mess. 我认为这会造成混乱。 The class column's value of your loaded locality is "Country" right? 加载位置的类列的值是“国家”吗?

So the problem was column named class , which ruby obviously hates. 所以问题是列名为class ,ruby显然很讨厌。

Solution posted in this StackOverflow question: Legacy table with column named "class" in Rails 此StackOverflow问题中发布的解决方案: Rails中名为“class”的列的旧表

or more specifically in this blog post (Accessed 25.03.2013): http://kconrails.com/2011/01/28/legacy-database-table-column-names-in-ruby-on-rails-3/ 或者更具体地说,在这篇博客文章中(访问日期: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.

相关问题 模块未在控制器中加载(undefined_method错误) - module not loading in the controller (undefined_method error) 使用资源在form_with Rails 5.1.6上显示“ Undefined_method,您的意思是[复数]路径吗?”错误消息 - “Undefined_method, did you mean [pluralised] path?” error message on form_with Rails 5.1.6, using resources Rails 4销毁action,undefined_method message_path - Rails 4 destroy action, undefined_method message_path Rails 6.0.4 config.ru undefined_method load_server - Rails 6.0.4 config.ru undefined_method load_server Rails教程will_paginate会引发undefined_method total_pages - Rails tutorial will_paginate raises undefined_method total_pages ActiveAdmin:date_range筛选器类型导致undefined_method错误(MetaSearch) - ActiveAdmin :date_range filter type causes undefined_method error (MetaSearch) 升级后出现奇怪的Rails 3.1+错误:未定义的方法“ parent_table_name” - Strange Rails 3.1+ error after upgrade: undefined method 'parent_table_name' 切换到Postgres后,Ruby on Rails应用程序中出现奇怪的“未定义方法”错误 - Strange “undefined method” error in Ruby on Rails app after switching to Postgres Rails Scaffold问题#undefined方法`edit_pais_path' - Rails Scaffold problem # undefined method `edit_pais_path' 得到奇怪的'未定义方法'错误 - Getting strange 'undefined method ' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM