简体   繁体   中英

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 :

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:

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:

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'.

I'm using rails 3.2.13 and a MySQL Database.

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?

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.

Solution posted in this StackOverflow question: Legacy table with column named "class" in Rails

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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