2016-04-05 1 views
1

ある場合:Railsの4 has_manyの関連はないワーキングモデルは2つのサブクラス私はRailsの</p> <p>4.次のように私は、以下のクラスから継承したアルゴンと呼ばれる異常レコードクラスを持って使用してい

class RecordBase < ActiveRecord::Base 
    self.abstract_class = true 
end 

class ArAndEcrBase < RecordBase 
    self.abstract_class = true 

    # Relations 
    belongs_to :originator, class_name: 'User', foreign_key: 'originator_id' 
    has_many :attachments 
end 

class Ar < ArAndEcrBase 

end 

IをArサブクラスの別のタイプのレコードを扱うクラスといくつかの関係を共有したいが、has_many関係は機能しない。

次作品:

> Ar.last.originator 
=> #<User id: 1, ... 

次のクラッシュ:has_manyの関係がうまく動作しないいくつかの理由

> Ar.last.attachments 
Mysql2::Error: Unknown column 'attachments.ar_and_ecr_base_id' 

ないattachments.ar_and_ecr_base_idをattachments.ar_idそれは列探さなければならない私が何か​​間違ったことをやっていますか?それともRailsのバグですか?

class Ar < ArAndEcrBase 
    has_many :attachments  
end 
+0

'inverse_of'オプションを使用してみましたか? – SomeSchmo

+0

こんにちはロッコ!その選択肢について聞いたことはありませんでした。それを設定しようとしたが動作しませんでした。私はまったく同じエラーを受け取りましたありがとう、とにかく! – nuno

+0

Atmコードを動作させる唯一の方法は、has_many関係をArクラスに移動することです: – nuno

答えて

0

あなたはおそらくpolymorphic association

を必要とするいくつかのモデルが同じ他のモデルへの関連付けを持たせたい場合は:

気圧コード作業を取得する唯一の方法は、ARクラスにhas_manyの関係を移動することです

class Picture < ActiveRecord::Base 
    belongs_to :imageable, polymorphic: true 
end 

class Employee < ActiveRecord::Base 
    has_many :pictures, as: :imageable 
end 

class Product < ActiveRecord::Base 
    has_many :pictures, as: :imageable 
end 
+0

こんにちは!私は多相関連を望んでいません。最初の2つのクラスはどのテーブルにも関連付けられていないことに注意してください。self.abstract_class = true、Arクラスだけが実際にテーブルを持っています。しかし、フィードバックのためのThx! – nuno

関連する問題