2013-02-25 54 views
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))} 

我與上面的努力,但我需要在我的結果設置得匹配的協議..怎樣包括在結果中加入記錄使用squeel

因爲這

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

我無法使用products.first.agreement.first ....這可能是一個不同的協議。

+0

這是最接近的: Product.joins {agreements} .where {agreements.id.in(a)&(short_description.like(st)| long_description.like(st))}。select {[agreements.agreement_type ,id]} 但不是id我希望從產品的一切 – Boti 2013-02-25 18:06:55

回答

0

我不知道如果這正是你所需要的,但希望這將是:

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

這將返回與作爲的協議,反過來,關聯到你的客戶相關的產品。如果你需要獲得一個客戶的協議,以一個產品,你可以寫一個範圍:

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) 

我希望這有助於。

相關問題