Scoping the results for rails3 jquery autocomplete plugin
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.
Rails gem rails3-jquery-autocomplete how to scope by user
In posts_controller
:
def get_autocomplete_items(parameters)
items = super(parameters)
items = items.where(:user_id => current_user.id)
end
I'm first calling the original get_autocomplete_items method, and then filtering out the results by current_user.id.
This question helped:
Rails 3: alias_method_chain still used?
Limit scope on rails-jquery-autocomplete (rails3-jquery-autocomplete) gem - where clause issue
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))
Rails Autocomplete Jquery (crowdint): select .uniq not working in scope, avoiding duplicates
The query that is generated by that gem:
Company Load (4.0ms) SELECT DISTINCT companies.name, companies.id FROM "companies" WHERE (LOWER(companies.name) ILIKE 'merce%') ORDER BY companies.name ASC LIMIT 10
Will always include the table primary id plus the name of the field(s) that will be auto-completed, the answer to that lies in lines 36 and 37 of this method:
def get_autocomplete_select_clause(model, method, options)
table_name = model.table_name
(["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + (options[:extra_data].blank? ? [] : options[:extra_data]))
end
That query results are already unique (it uses the distinct name + id). I can think of two possible ways to work around this:
Create a query with a subquery, the idea would be to create a first query that returns unique results then outer join it with the table ids and that will return unique names with its ids.
In your controller redefine the action that retrieve the results so that it do whatever you want. Actually what this gem does is to add the autocomplete_model_attribute action to your controller using meta programming (see source code).
Hope this helps let me know if this helps. Let me know if you need further details.
Modifying a method and using params in Rails3
You just made me realize that overriding get_items is not as flexible as it should be.
Your code is missing the use of parameters[:terms]
Something like this should make it work:
def get_items(parameters)
Post.where(:user_id => current_user.id).where(['title LIKE ?', "#{parameters[:term]}%"]
end
This is totally unfriendly, I'll try and work on something that makes more sense
Or, maybe a :scope option for the autocomplete declaration makes more sense?
Rails gem rails3-jquery-autocomplete: How do I query multiple fields
Your pseudo attribute works only on records already retrieved, but it has no bearing on searching for records. Probably the easiest solution is a named named scope like:
scope :search_by_name, lambda { |q|
(q ? where(["first_name LIKE ? or last_name LIKE ? or concat(first_name, ' ', last_name) like ?", '%'+ q + '%', '%'+ q + '%','%'+ q + '%' ]) : {})
}
Thus, a call like:
Person.search_by_name(params[:q])
will return an appropriate result set. It will also return all entries if no param was passed (or more specifically, this scope will add nothing extra), making it an easy drop-in for the index action.
Related Topics
How to Replace All Line Breaks in a String With ≪Br /≫ Elements
How to Determine If a Variable Is 'Undefined' or 'Null'
Difference Between Variable Declaration Syntaxes in JavaScript (Including Global Variables)
Tainted Canvases May Not Be Exported
Black and White Text Based on Background Image with CSS
Padding or Margin Value in Pixels as Integer Using Jquery
Programmatically Changing Webkit-Transformation Values in Animation Rules
Fastest Method to Replace All Instances of a Character in a String
Converting JavaScript Object With Numeric Keys into Array
Event.Preventdefault() Function Not Working in Ie
Ellipsis in the Middle of a Text (MAC Style)
Jspdf Can't Get Any Styling to Work
Am I Using Too Much Jquery? When am I Crossing the Line
Sending Command Line Arguments to Npm Script
What Does It Mean Global Namespace Would Be Polluted
Window Is Not Defined in Next.Js React App
How to Check Whether a Radio Button Is Selected With JavaScript