Guest User

Untitled

a guest
Jul 23rd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. # let's say you have an Account model with first name and last name as its fields.
  2. #
  3. # I would put a method that returns both names like so:
  4. #
  5.  
  6. class Account < ActiveRecord::Base
  7. def full_name
  8. "#{self.first_name} #{self.last_name}"
  9. end
  10. end
  11.  
  12. #
  13. # Then, on the autocomplete line, add that method as the source of the text:
  14. #
  15.  
  16. autocomplete :account, :first_name, :display_value => :full_name
  17.  
  18. #
  19. # :first_name won't matter because you'll override it later
  20. #
  21.  
  22. # Then, on your Controller, override get_autocomplete_items to:
  23.  
  24. class SomeController < ApplicationController
  25. autocomplete :account, :first_name, :display_value => :full_name
  26.  
  27. def get_autocomplete_items(parameters)
  28. Account.where("first_name LIKE ? OR last_name LIKE ?", "#{parameters[:term]}%").order(:first_name).order(:last_name)
  29. end
  30. end
Add Comment
Please, Sign In to add comment