1

在我的 ActiveRecord 查询中,我需要在 select 方法中提供此信息:

(SELECT count(*) from likes where likes.spentit_id = spentits.id) as like_count,
(SELECT count(*) from comments where comments.spentit_id = spentits.id) as comment_count

当然,我将这两个作为字符串传递给 .select() 部分,但我想知道这样做的正确/替代方法是什么?

这是我试图调用的完整查询:

SELECT DISTINCT
    spentits.*,
    username,
    (SELECT count(*) from likes where likes.spentit_id = spentits.id) as like_count,
    (SELECT count(*) from comments where comments.spentit_id = spentits.id) as comment_count,
    (SELECT count(*) from wishlist_items where wishlist_items.spentit_id = spentits.id) as wishlist_count,
    (case when likes.id is null then 0 else 1 end) as is_liked_by_me,
    (case when wishlist_items.id is null then 0 else 1 end) as is_wishlisted_by_me,
    (case when comments.id is null then 0 else 1 end) as is_commented_by_me
FROM spentits
LEFT JOIN users ON users.id = spentits.user_id
LEFT JOIN likes ON likes.user_id = 9 AND likes.spentit_id = spentits.id
LEFT JOIN wishlist_items ON wishlist_items.user_id = 9 AND wishlist_items.spentit_id = spentits.id
LEFT JOIN comments ON comments.user_id = 9 AND comments.spentit_id = spentits.id
WHERE spentits.user_id IN
    (SELECT follows.following_id
     FROM follows
     WHERE follows.follower_id = 9 AND follows.accepted = 1)
ORDER BY id DESC LIMIT 15 OFFSET 0;

这里的所有表都有各自的 ActiveRecord 对象。只是真的很困惑如何通过编写最少的 SQL 将此查询转换为“activerecord”/rails 方式。'9' user_id 应该是一个参数。

更新: 好的,这就是我同时做的事情,它比原始 SQL 语句要好得多,但对我来说仍然很难看:

class Spentit < ActiveRecord::Base
  belongs_to :user
  has_many :likes
  has_many :wishlist_items
  has_many :comments

  scope :include_author_info, lambda {
    joins([:user]).
    select("username").
    select("users.photo_uri as user_photo_uri").
    select("spentits.*")
  }

  scope :include_counts, lambda {
    select("(SELECT count(*) from likes where likes.spentit_id = spentits.id) as like_count").
    select("(SELECT count(*) from comments where comments.spentit_id = spentits.id) as comment_count").
    select("(SELECT count(*) from wishlist_items where wishlist_items.spentit_id = spentits.id) as wishlist_items_count").
    select("spentits.*")
  }
end

使用这些范围方法,我可以做到:

Spentit.where(:id => 7520).include_counts.include_author_info.customize_for_user(45)

稍微讲讲课。AUser有很多Spentits。A有Spentit很多comments和。likescomments

4

1 回答 1

0

好吧,你有点“做错了”。而不是

  scope :include_counts, lambda {
    select("(SELECT count(*) from likes where likes.spentit_id = spentits.id) as like_count").
    select("(SELECT count(*) from comments where comments.spentit_id = spentits.id) as comment_count").
    select("(SELECT count(*) from wishlist_items where wishlist_items.spentit_id = spentits.id) as wishlist_items_count").
    select("spentits.*")
  }

Spentit.find(7520).likes.count
Spentit.find(7520).wishlist_items.count
Spentit.find(7520).comments.count

代替

  scope :include_author_info, lambda {
    joins([:user]).
    select("username").
    select("users.photo_uri as user_photo_uri").
    select("spentits.*")
  }

Spentit.find(7520).user.username
Spentit.find(7520).user.photo_uri

此外,您可以在引用模型中定义范围,并使用这些范围:

class Follow < ActiveRecord::Base
  belongs_to :follower, :class_name => "User"
  belongs_to :following, :class_name => "User"

  scope :accepted, lambda{ where(:accepted => 1) }
end

Spentits.where(:user => Follow.where(:follower => User.find(9)).accepted)

现在,也许你也这样做:

class Spentit
  def to_hash
    hash = self.attributes
    hash[:like_count] = self.like.count
    # ...
  end
end

但是你不需要做任何花哨的事情来“在正常情况下”获得这些计数,你已经有了它们。

但是请注意,您可能还需要进行预先加载,您显然可以将其作为默认范围的一部分,否则您将执行比您需要的更多的查询。

于 2013-06-09T23:38:50.133 回答