2013-02-25 23 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) 

我希望这有助于。