Rails 3.2.11 MySql2 宝石
嗨,有人知道为什么我的 find 方法返回了错误类型的 activerecord 吗?
这是模型:
class NameList < ActiveRecord::Base
attr_accessible :name, :selected, :type
self.table_name='name_lists'
end
这是控制台输出:
>> k=NameList.find(28)
NameList Load (0.0ms) SELECT `name_lists`.* FROM `name_lists` WHERE `name_lists`.`id` = 28 LIMIT 1
#<Neighborhood id: 28, name: "Bayview">
>> k.class
Neighborhood(id: integer, city_id: integer, name: string, street_count: integer, relative_value: float, home_count: integer, min_lot_size: integer, created_at: datetime, updated_at: datetime)
请注意,我正在调用 NameList.find,但我得到的是 Neighborhood 对象。奇怪的是,sql 似乎是正确的——它查询 NameList 表。
Neighborhood 对象没有什么特别之处:下面是它的模型:
class Neighborhood < ActiveRecord::Base
belongs_to :city
has_many :streets
attr_accessible :name, :relative_value, :street_count
def self.make(name, relative_value, min_lot_size, street_count, home_count)
n=Neighborhood.new
n.name = name
end
end
但是,当我尝试保存它时-它使用 Neighborhood 模式的定义并尝试更新错误的表。
> k.name = "Foo"
"Foo"
>> k.save
(1.0ms) BEGIN
(0.0ms) UPDATE `neighborhoods` SET `name` = 'Foo', `updated_at` = '2013-03-14 17:40:46' WHERE `neighborhoods`.`id` = 28
(0.0ms) COMMIT
true
有任何想法吗?