Rails Gem Rails3-Jquery-Autocomplete How to Scope by User

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 gem to limit searches on locale parameter

The only way I see is to provide your own controller action method which is generated by rails-jquery-autocomplete gem by default.

class NationsController < ApplicationController

# you don't really need this anymore
# autocomplete :nationtranslation, :name

def autocomplete_nationtranslation_name
translations = Nationtranslation.where("name LIKE ? AND locale = ?",
"%#{params[:term]}%",
params[:locale])
.order(:name)
render json: translations.map { |t| {id: t.id, label: t.name, value: t.name} }
end
end

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.

Getting rails3-autocomplete-jquery gem to work nicely with Simple_Form with multiple inputs

First of all, this would be easier if the form is returning the ids instead of the name of the neighborhood. I haven't used the gem yet so I'm not familiar how it works. Reading on the readme says that it will return ids but i don't know why you're only getting names. I'm sure once you figure out how to return the ids, you'll be able to change the code below to suit that.

You need to create a join table between a neighborhood and a search. Let's call that search_neighborhoods.

rails g model search_neighborhood neighborhood_id:integer search_id:integer
# dont forget to add indexes in the migration

After that, you'd want to setup your models.

# search.rb
has_many :search_neighborhoods
has_many :neighborhoods, through: :search_neighborhoods

# search_neighborhood.rb
belongs_to :search
belongs_to :neighborhood

# neighborhood.rb
has_many :search_neighborhoods
has_many :searches, through: :search_neighborhoods

Now that we've setup the associations, we need to setup the setters and the attributes

# search.rb
attr_accessible :neighborhood_names

# this will return a list of neighborhood names which is usefull with prepopulating
def neighborhood_names
neighborhoods.map(&:name).join(',')
end

# we will use this to find the ids of the neighborhoods given their names
# this will be called when you call create!
def neighborhood_names=(names)
names.split(',').each do |name|
next if name.blank?
if neighborhood = Neighborhood.find_by_name(name)
search_neighborhoods.build neighborhood_id: neighborhood.id
end
end
end

# view
# you need to change your autocomplete to use the getter method
<%= f.input :neighborhood_names, url: autocomplete_neighborhood_name_searches_path, as: :autocomplete, input_html: { data: { delimiter: ',', multiple: true, class: "span8" } %>

last but not the least is to update find_listings

def find_listings
key = "%#{keywords}%"
listings = Listing.order(:headline).includes(:neighborhood)

if keywords.present?
listings = listings.where("listings.headline LIKE :key OR neighborhoods.name LIKE :key", { key: "#{keywords}")
end

if neighborhoods.exists?
listings = listings.where(neighborhood_id: neighborhood_ids)
end

listings
end

And that's it :)

UPDATE: using f.input_field

# view
<%= f.input_field :neighborhood_names, url: autocomplete_neighborhood_name_searches_path, as: :autocomplete, data: { delimiter: ',' }, multiple: true, class: "span8" %>

# model
# we need to put [0] because it returns an array with a single element containing
# the string of comma separated neighborhoods
def neighborhood_names=(names)
names[0].split(',').each do |name|
next if name.blank?
if neighborhood = Neighborhood.find_by_name(name)
search_neighborhoods.build neighborhood_id: neighborhood.id
end
end
end

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:

  1. 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.

  2. 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.

rails3-jquery-autocomplete redirect on select

The best way to perform re-direct when a user selects one of the drop down items from the autocomplete list when using rails3-jquery-autocomplete is via the "railsAutocomplete.select" event, which is triggered when the user selects one of the options.

How to do this is documented here. This approach means you can avoid editing the rails3-jquery-autocomplete code, and only causes a re-direct to happen when the user selects from the drop down option, rather than "onChange" of the form field, which isn't really what you want.



Related Topics



Leave a reply



Submit