How to filter out certain objects in a table with certain ids? (Rails 4, Postgresql) -
before ask question, provide context.
i have 3 models interact each other.
scoreboard has many teams. team belongs scoreboard.
the following use through
table.
scoreboard many-to-many user.(let's call scoreboard.followers) team many-to-many user.(let's call team.members)
i have team_members controllers following method:
team_member#new method:
def new @selected = true @scoreboard = scoreboard.find(params[:scoreboard_id]) @team = team.find(params[:team_id]) @team_members = @team.members @followers = @scoreboard.followers.search(params[:search]).paginate(page: params[:page], per_page: 50) end
what i'm attempting display scoreboard followers
in new.html.erb view. can search through them , add members team
associated same scoreboard.
here's new.html.erb view:
<%= form_tag(new_scoreboard_team_team_member_path, :method => "get", id: "member-search-form", autocomplete: "off") %> <div class="row new-member-field"> <div class="col-xs-12 col-sm-6"> <%= text_field_tag :search, params[:search], placeholder: "filter members name", class:"form-control" %> </div> </div> <% end %> <% if @followers.any? %> <% @followers.each |follower| %> <div class="row member-follower-div" id="follower_<%=follower.id%>"> <div class="col-xs-10 col-sm-5 member-prof-link"> <%= link_to follower.name, user_path(follower.id) %> </div> <div class="col-xs-2 col-sm-1 member-add"> <%= link_to (add_scoreboard_team_team_member_path(@scoreboard,@team, follower)), method: :post, remote: true, :data => {:disable_with => ".."} %> <i class="fa fa-plus-square fa-lg" aria-hidden="true"></i> <% end %> </div> </div> <% end %> <div class="row"> <div class="col-xs-12 col-sm-6 new-member-pages"> <%= will_paginate @followers %> </div> </div> <% end %>
okay question. problem arises in controller method. @team.members , @scoreboard.followers both source objects users table. how filter out user objects @scoreboard.followers exist in @team.members? don't want display @followers on view part of @team.members collection. also, bonus, alphabetically order followers user.name.
i tried .where("users.id not in (?)", @team_members.pluck(:id))
if @team_members collection null, no results displayed on page.
i solved so:
def new @selected = true @scoreboard = scoreboard.find(params[:scoreboard_id]) @team = team.find(params[:team_id]) @team_members = @team.members if (@team_members.any?) @wonderful_humans = @scoreboard.favourited_by.where("users.id not in (?)", @team_members.pluck(:id)).search(params[:search]).sort_by{ |a| a.name.downcase } else @wonderful_humans = @scoreboard.favourited_by.search(params[:search]).sort_by{ |a| a.name.downcase } end @followers = @wonderful_humans.paginate(page: params[:page], per_page: 50) end
Comments
Post a Comment