Check out this example of a Rails model:
class Customer < ActiveRecord::Base
attr_accessible :firstname, :lastname, :email, :phonenumber, :company_id
def name
"#{self.lastname} #{self.firstname}"
end
scope :search_by_name, lambda { |q|
(q ? where(["firstname LIKE ? or lastname LIKE ? or (firstname || ' ' || lastname) like ?", '%'+ q + '%', '%'+ q + '%','%'+ q + '%' ]) : {})
}
end
When I retrieve a JSON object of that model using ajax, I face a challenge. I need to access the name attribute in my autocomplete text field without combining firstname and lastname in one database field.
Do you have any suggestions on how to access the name attribute in JavaScript or how to include the name attribute in the JSON object? I'm looking for the most efficient method.