【问题标题】:How do I include the joined record in the result using squeel如何使用 squeel 在结果中包含连接的记录
【发布时间】:2013-02-25 17:17:14
【问题描述】:
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.... 那可能是不同的协议。

【问题讨论】:

  • 这是最接近的:Product.joins{agreements}.where{agreements.id.in(a) & (short_description.like(st) | long_description.like(st))}.select{ [agreements.agreement_type, id]} 但我想要的不是 id,而是 product 的所有内容

标签: ruby-on-rails activerecord squeel


【解决方案1】:

我不确定这是否正是您所需要的,但希望是:

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)

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2021-12-08
    相关资源
    最近更新 更多