如何从 Rails 2 中不包含特定项目的多对多关系中选择特定记录?

发布于 2024-10-07 12:46:24 字数 862 浏览 0 评论 0原文

我被这个问题困扰了几个小时。这可能是一个快速的解决方案,但我的大脑现在过热了。好的,就在这里。

我有 Session 和 SessionType 模型,它们之间具有多对多关系,如下所示。

class Session < ActiveRecord::Base
  ...
  has_and_belongs_to_many :session_types
  ...
end

class SessionType < ActiveRecord::Base
  ...
  has_and_belongs_to_many :sessions
  ...
end

我想要的是获得一个不包含任何特定 session_type 的会话,例如,

Session.find(:all, :joins => [:session_types], :conditions => ["session_types.id <> 44"])

它对我不起作用,因为上面的查询仍然会给我在许多关联中具有 session_types.id“44”的会话由于多对多关系的本质。

另外下面的 mysql 代码也不起作用。

select sessions.* from sessions
INNER JOIN `session_types_sessions` ON `session_types_sessions`.session_id = `sessions`.id 
WHERE (  session_types_sessions.session_type_id NOT IN (44))
GROUP BY sessions.id

任何帮助将不胜感激。

谢谢。

I am stuck with this for a couple of hours. It could be a quick solution but my brain is overheated now. Ok here it is.

I have Session and SessionType models which have many-to-many relationships to each other as follows.

class Session < ActiveRecord::Base
  ...
  has_and_belongs_to_many :session_types
  ...
end

class SessionType < ActiveRecord::Base
  ...
  has_and_belongs_to_many :sessions
  ...
end

What I want is to get a session which doesn't contain any specific session_type, eg.,

Session.find(:all, :joins => [:session_types], :conditions => ["session_types.id <> 44"])

It doesn't work for me since the above query will still give me the sessions which have session_types.id "44" in many of its associations because of the nature of many-to-many relationships.

Also the following mysql code doesn't work as well.

select sessions.* from sessions
INNER JOIN `session_types_sessions` ON `session_types_sessions`.session_id = `sessions`.id 
WHERE (  session_types_sessions.session_type_id NOT IN (44))
GROUP BY sessions.id

Any help will be appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜巴黎 2024-10-14 12:46:24

首先选择类型为 44 的会话:

session_types_44 = Session.find(:all, :joins => :session_types, 
  :conditions => { :"session_types.id" => 44 }).uniq

并选择不属于上述类型的会话:

sessions_without_type_44 = Session.find(:all, 
  :conditions => "id NOT IN (#{session_types_44.join(",")})")

您需要小心,因为如果 session_types_44 是空数组,您将收到 SQL 错误。

并回答您的第二问题:

你知道我如何更改 SQL 来过滤“获取具有 session_type_id '43' 的会话,但它们不能具有 '44'

sessions_without_type_44 获取结果并使用它。首先选择 SessionType使用 43 并通过关联获取属于该 SessionType 的所有会话,并且它们的 id 位于 sessions_without_type_44 内:

SessionType.find(43).sessions.all(:conditions => { 
  :id => sessions_without_type_44 })

First select those sessions which are of type 44:

session_types_44 = Session.find(:all, :joins => :session_types, 
  :conditions => { :"session_types.id" => 44 }).uniq

and select sessions which do not fall into the above:

sessions_without_type_44 = Session.find(:all, 
  :conditions => "id NOT IN (#{session_types_44.join(",")})")

You need to be careful with that because if session_types_44 is empty array you will get SQL error.

And to answer your second question:

do you know how I could change the SQL to filter like "get me sessions which have session_type_id '43', but they must NOT have '44'

Take the result from sessions_without_type_44 and use it. First select SessionType with 43 and through association get all sessions which belong to that SessionType and their ids are within the sessions_without_type_44:

SessionType.find(43).sessions.all(:conditions => { 
  :id => sessions_without_type_44 })
混浊又暗下来 2024-10-14 12:46:24

试试这个:

SELECT sessions.*
FROM sessions
LEFT JOIN session_types_sessions ON session_types_sessions.session_id = sessions.id AND session_types_sessions.session_type_id = 44
WHERE session_types_sessions.session_id IS NULL

Try this:

SELECT sessions.*
FROM sessions
LEFT JOIN session_types_sessions ON session_types_sessions.session_id = sessions.id AND session_types_sessions.session_type_id = 44
WHERE session_types_sessions.session_id IS NULL
野鹿林 2024-10-14 12:46:24

试试这个:

Session.all(
  :include => :session_types,
  :conditions => ["session_types.id IS NOT NULL AND session_types.id != ?", 44])
)

include 选项使用 LEFT JOIN,因此该查询将起作用。

Try this:

Session.all(
  :include => :session_types,
  :conditions => ["session_types.id IS NOT NULL AND session_types.id != ?", 44])
)

The include option uses LEFT JOIN hence this query will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文