1
我有三个型号:ActiveRecord的选择,通过多发HABTM
class User < ActiveRecord::Base
has_and_belongs_to_many :groups
end
class Group < ActiveRecord::Base
has_and_belongs_to_many :channels
has_and_belongs_to_many :users
end
class Channel < ActiveRecord::Base
has_and_belongs_to_many :groups
end
什么是最有效的方式来获得特定用户的所有通道(不重复)?
那么有效:
SELECT DISTINCT name FROM users
JOIN groups_users ON users.id=groups_users.user_id
JOIN channels_groups ON groups_users.group_id=channels_groups.group_id
JOIN channels ON channels_groups.channel_id=channels.id;
我爱红宝石。感谢你和&:快捷方式。 – Dave
不客气@Dave :-)如果它解决了你的问题,你应该接受答案;) – MrYoshiji
只需要注意,uniq!如果没有更改,则返回nil。然而,uniq在这个例子中工作正常。 – Dave