Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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 ActiveRecord.merge不处理两个关系_Sql_Ruby On Rails_Activerecord - Fatal编程技术网

Sql ActiveRecord.merge不处理两个关系

Sql ActiveRecord.merge不处理两个关系,sql,ruby-on-rails,activerecord,Sql,Ruby On Rails,Activerecord,我的应用程序中有以下型号: class Company < ActiveRecord::Base has_many :gallery_cards, dependent: :destroy has_many :photos, through: :gallery_cards has_many :direct_photos, class_name: 'Photo' end class Photo < ActiveRecord::Base belongs_to :galle

我的应用程序中有以下型号:

class Company < ActiveRecord::Base
  has_many :gallery_cards, dependent: :destroy
  has_many :photos, through: :gallery_cards
  has_many :direct_photos, class_name: 'Photo'
end

class Photo < ActiveRecord::Base
  belongs_to :gallery_card
  belongs_to :company
end

class GalleryCard < ActiveRecord::Base
  belongs_to :company
  has_many :photos
end
我尝试过使用
.merge()
方法(见下文),但它返回一个空关系。我认为原因在于选择
@company.photos
@company.direct\u photos
的条件不同。更详细地解释它

@company = Company.find(params[:id])
photos = @company.photos
direct_photos = @company.direct_photos
direct_photos.merge(photos) = []
photos.merge(direct_photos) = []
我还尝试了大量的
.joins
includes的组合,但没有成功

这可能是原始SQL查询的候选项,但我的SQL技能相当基本

值得一提的是,我重新研究了这个问题,并(在帮助下)提出了另一个查询,它一次捕获所有内容,而不是为第二个查询构建一个ID数组。这还包括其他联接表:

Photo.joins("
   LEFT OUTER JOIN companies ON photos.company_id = #{id}
   LEFT OUTER JOIN gallery_cards ON gallery_cards.id = photos.gallery_card_id
   LEFT OUTER JOIN quote_cards ON quote_cards.id = photos.quote_card_id
   LEFT OUTER JOIN team_cards ON team_cards.id = photos.team_card_id
   LEFT OUTER JOIN who_cards ON who_cards.id = photos.who_card_id
   LEFT OUTER JOIN wild_cards ON wild_cards.id = photos.wild_card_id"
   ).where("photos.company_id = #{id}
       OR gallery_cards.company_id = #{id}
       OR quote_cards.company_id = #{id}
       OR team_cards.company_id = #{id}
       OR who_cards.company_id = #{id}
       OR wild_cards.company_id = #{id}").uniq
ActiveRecord返回的是交叉点,而不是两个查询的并集,这与直觉相反

要查找联合,您需要使用
,ActiveRecord的内置支持很差。因此,我认为您是正确的,最好用SQL编写条件:

def all_photos
  Photo.joins("LEFT OUTER JOIN gallery_cards ON gallery_cards.id = photos.gallery_card_id")
    .where("photos.company_id = :id OR gallery_cards.company_id = :id", id: id)
end
ETA该查询将
gallery_卡
照片
关联,并将保留那些没有关联gallery卡行的照片行。然后,您可以基于
照片
列或相关的
图库卡
列进行查询–在这种情况下,可以从任一表中查询
公司id

您可以利用ActiveRecord作用域链接来连接和查询其他表:

def all_photos
  Photo.joins("LEFT OUTER JOIN gallery_cards ON gallery_cards.id = photos.gallery_card_id")
    .joins("LEFT OUTER JOIN quote_cards ON quote_cards.id = photos.quote_card_id")
    .where("photos.company_id = :id OR gallery_cards.company_id = :id OR quote_cards.company_id = :id", id: id)
end

我同意,这个名字很违反直觉。我尝试了您的查询,但遇到了一个错误:
PG::AmbiguousColumn:错误:列引用“company\u id”不明确,第1行:。。。gallery\u cards.id=photos.gallery\u card\u id WHERE(company\u id…
好的,我想我现在知道了。我只需要在WHERE子句中将
照片
预先添加到
company\u id
中。谢谢!如果公司通过多个联接表(gallery\u cards,quote\u cards,等等)拥有多张照片,我如何扩展查询?再次感谢!我用最终使用的查询编辑了我的原始帖子。之前我通过运行几个查询来构建ID数组,这些ID被传递到最终查询,但这是非常糟糕的代码。
def all_photos
  Photo.joins("LEFT OUTER JOIN gallery_cards ON gallery_cards.id = photos.gallery_card_id")
    .joins("LEFT OUTER JOIN quote_cards ON quote_cards.id = photos.quote_card_id")
    .where("photos.company_id = :id OR gallery_cards.company_id = :id OR quote_cards.company_id = :id", id: id)
end