【发布时间】:2011-11-01 21:45:47
【问题描述】:
我有一个 Rails 应用程序,它将位于一个遗留数据库之上,其中包含一些我必须处理的丑陋表。一个是与features 相关的feature_attributes 表。问题是这个feature_attributes 表没有主键。我不认为这会是一个问题,但显然它是。我的模型名称与表名称不同,但我使用 set_table_name 指定正确的名称。
class Feature < ActiveRecord::Base
has_many :feature_attributes
end
class FeatureAttribute < ActiveRecord::Base
set_table_name 'feature_attribute'
belongs_to :feature
end
一旦我加载了一个我知道与feature_attributes 相关的功能并在其上调用feature.feature_attributes,我就会得到nil。事实上,即使FeatureAttribute.first 给了我nil。 FeatureAttribute.any? 的结果返回 false。我担心 ActiveRecord 没有从表中读取任何数据,因为没有主键。这是怎么回事?
feature_attribute 表具有以下列。
feature_id
attribute_name
attribute_value
created_date
modified_date
救命!
我还会注意到,直接针对 MySQL 服务器运行生成的 SQL 实际上得到了我想要的行。
SELECT `feature_attribute`.* FROM `feature_attribute` WHERE `feature_attribute`.`feature_id` = 24;
编辑:非常抱歉。下次我将学习检查我的database.yml。显然,我正在从具有相同特征表的测试数据库中读取数据,但 feature_attribute 表完全是空的。
我觉得自己像个白痴。谢谢大家的帮助;我赞成大家为你们的麻烦投票。我确实喜欢几乎每个人的回答。 (我可以自己投反对票吗?:))
【问题讨论】:
标签: ruby-on-rails database activerecord