我好像不明白这个协会应该是什么样子的。我知道我没有像rails那样使用命名约定,但是我获取数据的源数据库也不是,我没有自由更改主键,因为它们也与真实的源数据库相关,我使用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)

下面是我返回的错误,看起来active record正在执行一个列名为空的查询:
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'

最佳答案

我想您需要为每个表设置正确的主键,所以我认为它应该是这样的。

class Eclass < ActiveRecord::Base
  set_primary_key :concept_id
  has_many :elinks, :foreign_key => :concept_id
  has_many :eproperties, :through => :elinks
end

class Elink < ActiveRecord::Base
  belongs_to :eclass, :foreign_key => :concept_id, :primary_key => :concept_id
  belongs_to :eproperty, :foreign_key => :pconcept_id, :primary_key => :pconcept_id
end

class Eproperty < ActiveRecord::Base
  set_primary_key :pconcept_id
  has_many :elinks, :foreign_key => :pconcept_id
  has_many :eclasses, :through => :elinks
end

10-06 09:24