0
st = 'pen'
ak = '123123'
agreements = Client.where{authentication_key == ak}.first.agreements
products = Product.joins{agreements}.where{agreements.id.in(a) & (short_description.like(st) | long_description.like( st))}

上記を試していますが、結果セットにも一致する一致が必要です..

このため

class Product < ActiveRecord::Base
  has_and_belongs_to_many :agreements, uniq: true

products.first.agreement.first... は使えません。別の契約かもしれません。

4

1 に答える 1

0

これがまさにあなたが必要とするものかどうかはわかりませんが、うまくいけば次のようになります。

client = Client.where { authentication_key == ak }.first
products = Client.agreements.joins { products }.where { (short_description.like(st) | long_description.like(st) }

これにより、クライアントに関連付けられている契約書に関連付けられた製品が返されます。製品について 1 人のクライアントの同意を得る必要がある場合は、スコープを記述できます。

class Product < ActiveRecord::Base
  #...
  def self.agreements_for_client(id)
    joins { agreements }.where { client_id.eq(id) }
  end
end

そしてそれを呼び出します:

client = Client.where { authentication_key == ak }.first
Product.first.agreements_for_client(client.id)

これが役立つことを願っています。

于 2013-02-25T18:15:34.077 に答える