[英]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.