我似乎无法理解这个协会的外观和工作方式。我知道我并没有像命名约定那样非常规矩,但是我获取数据的源数据库也不是,我没有更改主键的自由,因为它们也与我使用的真实源数据库相关ETL 加载到我的 Rails 数据库中。
这些是我的模型:
class Eclass < ActiveRecord::Base
has_many :elinks, :foreign_key => :concept_id
has_many :eproperties, :through => :elinks
end
class Elink < ActiveRecord::Base
belongs_to :eclass, :foreign_key => :concept_id
belongs_to :eproperty, :foreign_key => :pconcept_id
end
class Eproperty < ActiveRecord::Base
has_many :elinks, :foreign_key => :pconcept_id
has_many :eclasses, :through => :elinks
end
这些是 MySQL 表模式:
mysql> DESC eclasses;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| class_id | varchar(255) | YES | | NULL | |
| concept_id | varchar(255) | NO | PRI | 1 | |
| concept_name | varchar(255) | YES | | NULL | |
| language_id | varchar(255) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql> DESC elinks;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| schema_id | varchar(255) | YES | | NULL | |
| concept_id | varchar(255) | YES | | NULL | |
| pconcept_id | varchar(255) | YES | | NULL | |
| data_type | varchar(255) | YES | | NULL | |
| language_id | varchar(255) | YES | | NULL | |
| sequence | int(11) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
9 rows in set (0.02 sec)
mysql> DESC eproperties;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| property_id | varchar(255) | YES | | NULL | |
| pconcept_id | varchar(255) | NO | PRI | 1 | |
| property_name | varchar(255) | YES | | NULL | |
| language_id | varchar(255) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
这是我回来的错误,看起来活动记录正在执行一个带有空列名的查询:
irb(main):002:0> @eclass.eproperties
←[1m←[35mEproperty Load (0.0ms)←[0m SELECT `eproperties`.* FROM `eproperties`
INNER JOIN `elinks` ON `eproperties`.`` = `elinks`.`pconcept_id` WHERE `elinks`
.`eclass_id` IS NULL
Mysql2::Error: Unknown column 'elinks.eclass_id' in 'where clause': SELECT `epro
perties`.* FROM `eproperties` INNER JOIN `elinks` ON `eproperties`.`` = `elinks`.`pconcept_id` WHERE `elinks`.`eclass_id` IS NULL
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'elinks.eclass_id'
in 'where clause': SELECT `eproperties`.* FROM `eproperties` INNER JOIN `elinks` ON `eproperties`.`` = `elinks`.`pconcept_id` WHERE `elinks`.`eclass_id` IS NULL
我想返回给定类的属性。
更新:我为每个答案添加了外键声明。但仍然收到类似的错误。这是新的错误。
irb(main):001:0> @eclass = Eclass.first
←[1m←[36mEclass Load (140.6ms)←[0m ←[1mSELECT `eclasses`.* FROM `eclasses` LIMIT 1←[0m
=> #<Eclass id: 1, class_id: "0161-1#TM-005740#1", concept_id: "0161-1#01-004609#1", concept_name: "ACTUATOR ASSEMBLY,STEERING COLUMN", language_id:
"0161-1#LG-000001#1", created_at: "2009-06-08 20:28:00", updated_at: "2009-02-03 08:14:00">
irb(main):002:0> @props = @eclass.eproperties
←[1m←[35mEproperty Load (0.0ms)←[0m SELECT `eproperties`.* FROM `eproperties` INNER JOIN `elinks` ON `eproperties`.`` = `elinks`.`pconcept_id` WH
ERE `elinks`.`concept_id` IS NULL
Mysql2::Error: Unknown column 'eproperties.' in 'on clause': SELECT `eproperties`.* FROM `eproperties` INNER JOIN `elinks` ON `eproperties`.`` = `el
inks`.`pconcept_id` WHERE `elinks`.`concept_id` IS NULL
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'eproperties.' in 'on clause': SELECT `eproperties`.* FROM `eproperties` INNER JOIN `e
links` ON `eproperties`.`` = `elinks`.`pconcept_id` WHERE `elinks`.`concept_id` IS NULL
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `query'
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `block in exec_qu
ery'
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
ect_all'
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
et'
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
from c:in `load_target'
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_r
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/comman
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/comman
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/comman
from script/rails:6:in `require'