gpt4 book ai didi

ruby-on-rails - 通过Ruby on Rails中的联接表选择记录

转载 作者:行者123 更新时间:2023-12-04 05:55:23 27 4
gpt4 key购买 nike

我有三种模式

class Collection < ActiveRecord::Base
has_many :presentations
has_many :galleries, :through => :presentations
end

class Gallery < ActiveRecord::Base
has_many :presentations
has_many :collections, :through => :presentations
end

class Presentation < ActiveRecord::Base
belongs_to :collection
belongs_to :gallery
end


如何获得不属于给定画廊的所有收藏?

我的SQL知识只是基本知识。我也想让Rails(2.3)完成工作而无需显式使用SQL表达式。

最佳答案

开箱即用,从技术上讲,您必须编写一些SQL(where子句)...

gallery_to_exclude = Gallery.first
Collection.find(:all,
:include => :presentations,
:conditions => ['presentations.gallery_id IS NULL OR presentations.gallery_id != ?',
gallery_to_exclude.id])


但是,如果要使用 Searchlogic,可以避免这种情况:

Collection.presentations_gallery_id_is_not(gallery_to_exclude.id).all

关于ruby-on-rails - 通过Ruby on Rails中的联接表选择记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3741754/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com