6

I'm using https://github.com/crowdint/rails3-jquery-autocomplete and it seems to be working. The only problem is lets say I have a field performing an autocomplete on all post titles, but I want it to only show titles of posts the user has created.

Is there a way to do something like this? I.e. a find_by or something?

Thanks!

-Elliot

EDIT

it says in the documentation:

If you want to display a different version of what you're looking for, you can use the :display_value option.

This options receives a method name as the parameter, and that method will be called on the instance when displaying the results.

class Brand < ActiveRecord::Base
  def funky_method
    "#{self.name}.camelize"
  end
end


class ProductsController < Admin::BaseController
  autocomplete :brand, :name, :display_value => :funky_method
end
In the example above, you will search by name, but the autocomplete list will display the result of funky_method

I feel like this could be used to make what I'm trying to accomplish happen, but I have no idea where to begin?

Elliot
  • 13,580
  • 29
  • 82
  • 118

3 Answers3

7

You can override get_autocomplete_items, but first use super to call the original get_autocomplete_items method, and then filter the results by current_user.id (or post.user.id if the owner of the post is not the current user)

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

This way you can still all the other options that come with the gem. This should be done in your controller by the way.

Another answer suggested the :scope option. I looked into that, but it is not the way to go in this case, you don't want to add "current_user" and things like that to your models.

deb
  • 12,326
  • 21
  • 67
  • 86
2

You should overwrite the method get_item, for example in these case I'm filtering the list of users to consider to just the last one:

class UsersController < ApplicationController
    autocomplete :user, :email, :full => true

    def index
        @users = User.all
    end

    def get_items(parameters)
        [User.last]
    end

I think that one of the last commits, changed the method (#get_items) name. Check you version of autocomplete.rb file.

Check it here: https://github.com/crowdint/rails3-jquery-autocomplete/commit/b3c18ac92ced2932ac6e9d17237343b512d2144d#L1R84.

Claudio Acciaresi
  • 31,951
  • 5
  • 33
  • 43
  • Hi ClaudioA,so this works to a degree: def get_items(parameters) Post.where(:user_id => current_user.id) end but the autocomplete no longer functions, it just shows a list of the items which meet those needs? – Elliot Feb 02 '11 at 01:28
  • you can still use the original get_items method by calling super, more details on answer – deb Aug 30 '11 at 21:59
1

Even though I havent personally tested, I think normal scope method in rails3 should work. As after all scope is also a method same as "funky_method"

write a scope

in your model

scope :funky_method, lambda { |param1| :conditions => {:column = param1} }

and in your controller

autocomplete :brand, :name, :display_value => :funky_method

should work, try and see..

cheers

sameera

sameera207
  • 16,547
  • 19
  • 87
  • 152
  • Hi Sameera, I tried: scope :funky_method, lambda { where("post.user_id LIKE ?", current_user.id) } But it didn't seem to work...? – Elliot Feb 01 '11 at 12:13
  • I wasn't able to get scopes to work for this gem, sadly. I had to modify the get_autocomplete_method directly. The good news is that you can mod the get_autocomplete_items method using Rails' selector queries or straight up SQL. In this example, I'm looking for a first and last name match. https://gist.github.com/bb3b54f2ec793cf00fd0 – slant Mar 14 '11 at 00:23