2017-05-31 7 views
-1

私は複雑なPostgreSQL文字列をArelに翻訳しており、has_and_belongs_to_manyテーブルを正しく整形する方法を知りたいと思います。例えば、ここに関連したモデルは、次のとおりです。has_and_belongs_to_manyテーブルをArelに変換する正しい方法は何ですか?

class Release < ApplicationRecord 
    has_and_belongs_to_many :vulnerabilities 
end 

class Vulnerability < ApplicationRecord 
    has_and_belongs_to_many :releases 
end 

クラスの設定は表を作成しますreleases_vulnerabilitiesと呼ばれるが、私はARELに変換するように見えることはできません。私は自分のレールコンソールRelease_Vulnerability.arel_tableを試してみたのですが、Arel::Table.new(:releases_vulnerabilities)を試しましたが、2番目の実装では関連する外部キーを照会することができません。このテーブルをarelに変換する正しい方法は何ですか?

+1

なぜhas_many:releases、through:assembly_parts'を使ってみませんか?実際にAssemblyPartモデルを作成しないのに十分な理由はありますか?それは簡単にすべてをarelにすることを可能にします。また、なぜあなたはこの場合にあなたが必要と思いますか? – radha

+0

「2番目の実装では、関連する外部キーをクエリできません」という意味はどうですか? – chumakoff

+0

なぜあなたは最初にそれを「エイリアス化」したいのですか? Arelは本当に不安定なプライベートAPIであり、Railsのアップグレードで予告なしに変更されます。 – Iceman

答えて

0

あなたは完全にこの

class Release < ApplicationRecord 
    has_many :vulnerabilities 
    has_many :vulnerabilities, through: :release_vulnerabilities 
end 

class ReleaseVulnerabilities < ApplicationRecord 
    belongs_to :vulnerability 
    belongs_to :release 
end 

class Vulnerability < ApplicationRecord 
    has_many :release_vulnerabilities 
    has_many :releases, through: :release_vulnerabilities 
end 

にこの

class Release < ApplicationRecord 
    has_and_belongs_to_many :vulnerabilities 
end 

class Vulnerability < ApplicationRecord 
    has_and_belongs_to_many :releases 
end 

を変更することができますし、任意の関係を失うことはありません、あなたも@ vulnerability.releasesが、まだ動作しますので、コードを更新する必要がいけません、両方のクエリを見ると、どちらの場合でもその結合が表示されます。 "arielize"のアイデアは今見てくださいthis conversion I did

関連する問題