Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 基于多对象via的ActiveRecord查询有很多关系_Sql_Ruby On Rails_Ruby_Activerecord_Ruby On Rails 4 - Fatal编程技术网

Sql 基于多对象via的ActiveRecord查询有很多关系

Sql 基于多对象via的ActiveRecord查询有很多关系,sql,ruby-on-rails,ruby,activerecord,ruby-on-rails-4,Sql,Ruby On Rails,Ruby,Activerecord,Ruby On Rails 4,我有一个产品类,该类通过连接类实例具有许多性别。我想查询查找同时存在end_a和end_b的产品。当前类方法有两个警告: 如果搜索的end_a和end_b相同,则无法正确返回。相反,应该搜索product是否有两个实例,而不仅仅是一个对象 当我需要ActiveRecord\u关系时,返回一个数组 下面是课堂方法.query,欢迎反馈或想法 class Product < ActiveRecord::Base has_many :connections, dependent:

我有一个
产品
类,该类通过
连接
类实例具有许多
性别
。我想查询查找同时存在
end_a
end_b
的产品。当前类方法有两个警告:

  • 如果搜索的
    end_a
    end_b
    相同,则无法正确返回。相反,应该搜索
    product
    是否有两个实例,而不仅仅是一个对象
  • 当我需要
    ActiveRecord\u关系时,返回一个
    数组
下面是课堂方法
.query
,欢迎反馈或想法

class Product < ActiveRecord::Base

  has_many   :connections, dependent: :destroy, as: :connectionable
  has_many   :genders,     through:   :connections

  def self.query(end_a, end_b)
    search_base = active.joins(:connections)
    end_a_search = search_base.where(connections: { gender_id: end_a  } )

    end_a_search & search_base.where(connections: { gender_id: end_b  } )
  end
end
类产品
ps:一旦解决了这一问题,可能会将其转移到
产品

类产品class Product < ActiveRecord::Base
  has_many :connections, dependent: :destroy, as: :connectionable
  has_many :genders, through: :connections

  scope :with_genders, -> (end_a, end_b) {
    relation = joins('INNER JOIN connections c1 ON c1.connectionable_id = products.id AND c1.connectionable_type = \'Product\'')
      .joins('INNER JOIN connections c2 ON c1.connectionable_id = c2.connectionable_id AND c2.connectionable_type = \'Product\'')
      .where(c1: {gender_id: end_a}, c2: {gender_id: end_b})
      .group('products.id')
    end_a == end_b ? relation.having('COUNT(products.id) > 1') : relation
  }
end
有多个:连接、依赖::销毁、as::可连接 有很多:性别,通过::连接 经营范围:有性别,->(完a,完b){ relation=joins('c1.connectionable\u id=products.id和c1.connectionable\u type=\'Product\''上的内部连接c1) .joins('c1.connectionable\u id=c2.connectionable\u id和c2.connectionable\u type=\'Product\''上的内部连接c2) .其中(c1:{性别id:end_a},c2:{性别id:end_b}) .group('products.id') end_a==end_b?关系。having('COUNT(products.id)>1'):关系 } 结束
什么是连接?Join model?@BroiSatse修复了上述问题,但是
Genders
通过
连接属于
产品
范围:一些查询,->(end\u a,end\u b)产品。连接(:Connections)。其中(“Connections.gender\u id=”或Connections.gender\u id=?”,end\u a,end\b)
@bjhaid我认为这不可行,因为它会返回只与
end\u a
end\u b
有关系的产品实例。我需要产品与
end\u a
end\u b
都有关系。我很抱歉,
连接是多态的,因此当我尝试上述方法,甚至用
可连接的\u id
替换
产品\u id
时,如果搜索
end\u a
end_b
是相同的。请参阅更新的响应。今后,在你的问题中尽量提供所有必要的信息——这样做可以节省那些帮助你的人的时间。我很抱歉以前没有提到多态模型。但是我实现了您更新的作用域,并且得到一个错误
PG::UndefinedColumn:error:column“Product”不存在第1行
。感谢您迄今为止的所有帮助。感谢您的更新,现在代码正在运行,但没有涵盖我在最初的帖子中提到的警告:“如果搜索
end_a
end_b
相同,则无法正确返回。相反,应该搜索产品是否有2个实例,而不仅仅是一个对象。”你知道如何解决这个问题吗?对不起,可能我没有仔细阅读说明。看最后的更新,这是你需要的吗?