使用Ruby on Rails 3.2.13和Squeel我有以下模型:
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :groups
has_many :characters, :dependent => :destroy
end
class Character < ActiveRecord::Base
belongs_to :user
end
字符具有布尔属性:public
。
在Character模型中,我想检索当前用户可见的所有字符,具体取决于以下条件:
结果必须是ActiveRecord::Relation
。
匹配前两个条件很简单:
def self.own_or_public user_to_check
where{
(user_id == user_to_check.id) |
(public)
}
end
对于第三个条件,以下查询会产生正确的结果,但可能不是最好的方法:
def self.shares_group_with user_to_check
user_groups = Group.joins{users}.where{users.id == user_to_check.id}
joins{user.groups}.
where{
user.groups.id.in(user_groups.select(id))
}.uniq
end
此外,我找不到连接两个结果的方法,产生包含两个查询结果的ActiveRecord::Relation
(merge
产生与两个查询匹配的元素,+
返回{ {1}}而不是Array
)。
非常感谢任何有关如何在单个Squeel查询中处理此问题的帮助。
答案 0 :(得分:0)
让我们尝试重新解决您的问题并将has_and_belongs_to_many
关联替换为has_many, through
关联,我们将在has_many, through
模型上添加另一个Character
关联,如下所示:< / p>
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
class Group < ActiveRecord::Base
has_many :memberships
has_many :users, through: :memberships
end
class User < ActiveRecord::Base
has_many :memberships
has_many :groups, through: :memberships
has_many :characters
end
class Character < ActiveRecord::Base
belongs_to :user
has_many :groups, through: :user
end
Membership
模型表示User
和Group
之间的关系 - 本质上是使用has_and_belongs_to_many
时隐藏的连接表。我更喜欢看到这种关系(特别是如果它很重要)。
我们还将Character
模型与与用户关联的Group
关联起来。当我们尝试加入我们的范围时,这很有用。
将Character
模型清空,让我们添加以下内容:
sifter :by_user do |user|
user_id == user.id
end
sifter :public do
public
end
使用sifters作为构建块,我们可以添加以下内容以获取可见字符(如您所定义):
def self.get_visible(user)
Character.uniq.joins{groups.outer}.where{(sift :public)|(sift :by_user, user)|(groups.id.in(user.groups))}
end
此方法采用User
的实例并找到以下Character
s:
然后我们只从这些集合中获取不同的字符列表。
来自rails console:
irb(main):053:0> Character.get_visible(User.find(4))
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 4]]
Group Load (0.7ms) SELECT "groups".* FROM "groups" INNER JOIN "memberships" ON "groups"."id" = "memberships"."group_id" WHERE "memberships"."user_id" = 4
Character Load (0.9ms) SELECT DISTINCT "characters".* FROM "characters" LEFT OUTER JOIN "users" ON "users"."id" = "characters"."user_id" LEFT OUTER JOIN "memberships" ON "memberships"."user_id" = "users"."id" LEFT OUTER JOIN "groups" ON "groups"."id" = "memberships"."group_id" WHERE ((("characters"."public" OR "characters"."user_id" = 4) OR "groups"."id" IN (2)))
[
[0] #<Character:0x00000005a16b48> {
:id => 4,
:user_id => 4,
:name => "Testiculies",
:created_at => Tue, 13 Aug 2013 14:35:50 UTC +00:00,
:updated_at => Tue, 13 Aug 2013 14:35:50 UTC +00:00,
:public => nil
},
[1] #<Character:0x00000005d9db40> {
:id => 1,
:user_id => 1,
:name => "conan",
:created_at => Mon, 12 Aug 2013 20:18:52 UTC +00:00,
:updated_at => Tue, 13 Aug 2013 12:53:42 UTC +00:00,
:public => true
}
]
要查找特定User
具有的所有字符,请向User
模型添加实例方法:
def get_visible_characters
Character.get_visible(self)
end
我认为这会让你到达目的地。