我有一个 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
表完全是空的。
我觉得自己像个白痴。谢谢大家的帮助;我投票给你们所有的麻烦。我确实喜欢几乎每个人的回答。 (我可以自己投反对票吗?:))
最佳答案
尝试同时设置主键:
class FeatureAttribute < ActiveRecord::Base
set_table_name 'feature_attribute'
set_primary_key 'feature_id'
belongs_to :feature
end
更新
我认为您的问题出在其他地方。我刚刚测试过,ActiveRecords 可以很好地处理没有主键的表:
对于一个简单的表格:
class CreateThings < ActiveRecord::Migration
def change
create_table :things, :id => false do |t|
t.string :name
t.timestamps
end
end
end
在控制台中:
Loading development environment (Rails 3.1.1)
irb(main):001:0> Thing.create(:name=>'A name for the thing')
(0.1ms) BEGIN
SQL (0.3ms) INSERT INTO `things` (`created_at`, `name`, `updated_at`) VALUES ('2011-11-02 16:33:48', 'A name for the thing', '2011-11-02 16:33:48')
(40.3ms) COMMIT
=> #<Thing name: "A name for the thing", created_at: "2011-11-02 16:33:48", updated_at: "2011-11-02 16:33:48">
irb(main):002:0> Thing.first
Thing Load (0.7ms) SELECT `things`.* FROM `things` LIMIT 1
=> #<Thing name: "A name for the thing", created_at: "2011-11-02 16:33:48", updated_at: "2011-11-02 16:33:48">
irb(main):003:0>
更新 2 不太好:
irb(main):003:0> Thing.create(:name=>'Another thing')
(0.2ms) BEGIN
SQL (0.4ms) INSERT INTO `things` (`created_at`, `name`, `updated_at`) VALUES ('2011-11-02 16:40:59', 'Another thing', '2011-11-02 16:40:59')
(35.4ms) COMMIT
=> #<Thing name: "Another thing", created_at: "2011-11-02 16:40:59", updated_at: "2011-11-02 16:40:59">
irb(main):004:0> Thing.first
Thing Load (0.5ms) SELECT `things`.* FROM `things` LIMIT 1
=> #<Thing name: "A name for the thing", created_at: "2011-11-02 16:33:48", updated_at: "2011-11-02 16:33:48">
irb(main):005:0> Thing.last
Thing Load (11.8ms) SELECT `things`.* FROM `things` ORDER BY `things`.`` DESC LIMIT 1
Mysql2::Error: Unknown column 'things.' in 'order clause': SELECT `things`.* FROM `things` ORDER BY `things`.`` DESC LIMIT 1
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'things.' in 'order clause': SELECT `things`.* FROM `things` ORDER BY `things`.`` DESC LIMIT 1
关于ruby-on-rails - rails ActiveRecord : legacy table without primary key shows nil for result?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7973335/