Rails 4 - Thinking-Sphinx,在连接表中搜索条件

时间:2014-09-14 14:34:18

标签: sql ruby-on-rails ruby sphinx thinking-sphinx

我有四个模型,UserTeamIdeaMemberMemberTeamUser之间的联接表。我希望用户能够访问彼此的个人资料页面controller: :users, action: :show),在show视图中我要列出属于两个用户所属的团队的Ideas。我已经创建了一个相关问题,列出了正确的团队here

以下是我现在所拥有的:

def show

  @user = User.find(params[:id])

  # Only list teams that both users are members of
  @teams = @user.teams.find(@user.members.pluck(:team_id) & current_user.members.pluck(:team_id))

  # What should I do to list ideas that are created for teams the both users are members of?
  @ideas = @user.ideas.search(params[:search], conditions: { 'how should the condition look?' } :page => params[:page], :per_page => 10, :order => 'created_at DESC')

end

我的模型设置如下:

有两个模型,User和Team,它是一个多对多的连接表,名为Member。它设置如下:

#Team:
has_many :members, dependent: :destroy
has_many :users, through: :members 

#User
has_many :members
has_many :teams, through: :members

#Member
belongs_to :user
belongs_to :team

#Idea
belongs_to :user
belongs_to :team

关于我应该做什么的任何想法?

1 个答案:

答案 0 :(得分:1)

您需要Idea索引中的以下属性(如果您还没有):

has team_id

然后以下应该做的伎俩(给定控制器的现有上下文):

@ideas = Idea.search params[:search],
  :with     => {:team_id => @teams.collect(&:id)},
  :page     => params[:page],
  :per_page => 10,
  :order    => 'created_at DESC'

想要通过@user.ideas指导想法搜索 - 这会将其限制为仅附加到@user 的想法团队,而不是current_user的团队。