0

There is a recommended solution and it seems to work. The issue is in my where clause and I'm not sure what's wrong. For reference, here is the solution(s):

I am trying to scope users that are members of the current_user's family tree memberships(branches) user's within my Nodes controller. This would normally be done using this code (current_user.family_tree.memberships).

Note I have successfully set this up to autocomplete showing all users (User.all):

In my routes:

resources :nodes do
  get :autocomplete_user_first_name, :on => :collection
end  

In my Node controller I have the following code:

autocomplete :user, :first_name, :extra_data => [:last_name, :email],
 display_value: :full_name

And in my view I have the following form:

<%= form_for node do |f| %>
  <%= f.label :user_tags %>
  <%= f.autocomplete_field :user_tags, autocomplete_user_first_name_nodes_path, 'data-auto-focus' => true, value: nil %>
  <%= f.submit %>  
<% end %>

When I attempt to add the recommended solution to my nodes controller:

def get_autocomplete_items(parameters)
  items = super(parameters)
  items = items.where(:user_id => current_user.family_tree.memberships)
end

I get this message: NoMethodError - super: no superclass method "get_autocomplete_items" for #<NodesController:0x007fc516692278>:

So, I found this article https://stackoverflow.com/a/18717327/4379077 and changed it to

def get_autocomplete_items(parameters)
  items = active_record_get_autocomplete_items(parameters)
  items = items.where(:user_id => current_user.family_tree.memberships)
end

It works, but I get the following error PG::UndefinedColumn: ERROR: column users.user_id does not exist, so I changed the where clause to this :id => current_user.family_tree.memberships and I get this result

User Load (0.9ms)  SELECT  users.id, users.first_name, "users"."last_name",
"users"."email" 
FROM "users"  WHERE (LOWER(users.first_name) ILIKE 'mi%')
AND "users"."id" IN (SELECT "memberships"."id" FROM "memberships"  
WHERE "memberships"."family_tree_id" = $1)  
ORDER BY LOWER(users.first_name) ASC LIMIT 10  [["family_tree_id", 1]]

The issue is that I believe I need to get a collection within the membership model comparing the attribute membership.user_id to user.id. What am I doing wrong in my where clause?

Community
  • 1
  • 1
mike0416
  • 461
  • 4
  • 17

1 Answers1

3

Are Membership objects the same thing as Users?

if not, you need to get the user_id off the membership record

This line would need to change

# use pluck to get an array of user_ids.
items = items.where(:id => current_user.family_tree.memberships.pluck(:user_id))
Mark Swardstrom
  • 17,217
  • 6
  • 62
  • 70
  • 1
    I know I'm not supposed to thank you in comments @swards, but you saved my sanity. Thank you! – mike0416 Nov 03 '15 at 23:44
  • @swank, how would I add an AND onto this where clause where :full_name is NOT contained within the node.user_tag_list collection? – mike0416 Nov 04 '15 at 00:09
  • Try editing this `autocomplete :user, :first_name, :extra_data => [:last_name, :email], display_value: :full_name` to see if you can fix it. Configuring display value like it is will show the full name. More here I think: https://github.com/crowdint/rails3-jquery-autocomplete – Mark Swardstrom Nov 04 '15 at 05:25