多个连接查询以排除某些记录

时间:2015-03-06 17:24:14

标签: mysql sql ruby-on-rails ruby-on-rails-4 join

我有3个型号:

class SocialAccount < ActiveRecord::Base
  has_many :message_social_accounts
  has_many :messages, through: :message_social_accounts
end

class MessageSocialAccount < ActiveRecord::Base
  belongs_to :message
  belongs_to :social_account
end

class Message < ActiveRecord::Base
 has_many :message_social_accounts
 has_many :social_accounts, through: :message_social_accounts
end

拥有多个特定的社交帐户,我正在尝试确定其中哪些帐户尚未收到消息。 我正在尝试创建一个连接查询以从数据库中查找社交帐户: a)在[1..20]中有id b)没有任何message_social_account记录,消息id = 1

试过这个加入:

SocialAccount.select('social_accounts.*').joins(:message_social_accounts).group('social_accounts.id').where("social_accounts.id IN (#{potential_recipients.map{|x|x.id}.join(',')}) AND message_social_accounts.message_id IS NOT #{self.id}").to_a

但它没有返回所需的结果。可以提供线索吗?感谢

修改

结果创建了以下查询:

"SELECT social_accounts.* FROM \"social_accounts\" INNER JOIN \"message_social_accounts\" ON \"message_social_accounts\".\"social_account_id\" = \"social_accounts\".\"id\" WHERE (social_accounts.id IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) AND message_social_accounts.message_id IS NOT 1) GROUP BY social_accounts.id"

EDIT2 message_social_account记录的示例

#<MessageSocialAccount id: 4, message_id: 4, social_account_id: 4, read_time: "2000-01-01 12:06:40", created_at: "2015-02-20 20:43:46", updated_at: "2015-02-20 20:43:46", push_notification: nil>,
#<MessageSocialAccount id: 5, message_id: 5, social_account_id: 3, read_time: "2000-01-01 11:34:34", created_at: "2015-02-20 20:43:46", updated_at: "2015-02-20 20:43:46", push_notification: nil>,
#<MessageSocialAccount id: 6, message_id: 6, social_account_id: 2, read_time: "2000-01-01 02:13:25", created_at: "2015-02-20 20:43:46", updated_at: "2015-02-20 20:43:46", push_notification: nil>,
#<MessageSocialAccount id: 33, message_id: 33, social_account_id: 3, read_time: "2000-01-01 13:40:21", created_at: "2015-02-20 20:43:46", updated_at: "2015-02-20 20:43:46", push_notification: nil>,
#<MessageSocialAccount id: 36, message_id: 36, social_account_id: 4, read_time: nil, created_at: "2015-02-20 20:43:46", updated_at: "2015-02-20 20:43:46", push_notification: nil>,
#<MessageSocialAccount id: 37, message_id: 37, social_account_id: 2, read_time: nil, created_at: "2015-02-20 20:43:46", updated_at: "2015-02-20 20:43:46", push_notification: nil>,
#<MessageSocialAccount id: 42, message_id: 42, social_account_id: 3, read_time: "2000-01-01 09:32:45", created_at: "2015-02-20 20:43:46", updated_at: "2015-02-20 20:43:46", push_notification: nil>,
#<MessageSocialAccount id: 44, message_id: 45, social_account_id: 1, read_time: nil, created_at: "2015-03-02 16:07:31", updated_at: "2015-03-02 16:07:40", push_notification: true>,
#<MessageSocialAccount id: 45, message_id: 45, social_account_id: 2, read_time: nil, created_at: "2015-03-02 16:07:31", updated_at: "2015-03-02 16:07:31", push_notification: nil>,
#<MessageSocialAccount id: 46, message_id: 45, social_account_id: 3, read_time: nil, created_at: "2015-03-02 16:07:31", updated_at: "2015-03-02 16:07:31", push_notification: nil>

0 个答案:

没有答案