I’m attempting an auto_complete by user.name, but it’s not as simple as
it sounds. I’ve followed the great Railscast on the topic, and I do have
the ajaxiness working just fine when I search by ID (I was doing this
for testing purposes - I don’t want users using IDs to find each other,
luckily). My auto_complete field needs to work the same as the GitHub
‘Collaborator’ addition form (or the Facebook search), whereby we find
the user by user.name. My problem is that the user.name attribute is
only a method in my user.rb, it is not a unique attribute, nor does it
exist in the users table. So, we can’t identify a user through it, we
need the ID.
Further to this, the app has a Profile model for storing additional user
info (I’m beginning to hate this design - is it really necessary? I’m
only doing it because I was scared in to it from a security standpoint).
The Profile model has first_name and last_name. The User model only has
the :name attribute through the Profile model;
user.rb
def name
“#{profile.first_name} #{profile.last_name}”
end
So, on the front-end, my auto_complete form needs to find the users by
the user.name attribute, but when we select a user from the
auto_completed results, we also need to have the user.id hidden away
somewhere (a hidden field, perhaps?) whilst the user.name is displayed
in the auto_completed_text_field as visual feedback for the end user.
Any ideas on how I achieve this?