How to bring extra_data from join tables on rails3_jquery_autocomplete

I have a association User belongs_to Role and want to search users by name using rails3_jquery_autocomplete. And I want to show some values of Role after select a user.

Here is my controller

class UserController < ApplicationController
   autocomplete :user, :name, scopes: [:join_role], 
   extra_data: [:address, :email]   

And here are my models

class User < ActiveRecord::Base
  belongs_to :role
  attr_accessible :name, :address, :email
  scope :join_role, joins(:role)


class Role < ActiveRecord::Base
   attr_accessible :name, :level

How can I get the name and level values in the user autocomplete search?

I can't add :name, :level on the extra_data option neither :role because it will return a sql error.

Here rails3-jquery-autocomplete-search-by-several-fields says that I could override the get_autocomplete_items method but dont know how.

Something like that?

def get_autocomplete_items(parameters)
    super(parameters)
    result = []
    User.join_role.where('....').each do |user|
      result << {id: user.id, label: user.name, role: user.role.name, 
        address: user.address, email: user.email, level: user.role.level }
    end
    result
  end

But it result in "undefined method `id' for Hash..."

Answers


Need Your Help

How can I use C++ 11 features in Clang?

c++ c++11 clang

How can I use the latest C++ 11 features in Clang? What (sub)set of features is supported?

Optimal way to read out JSON from MongoDB into a Scalatra API

json mongodb scala scalatra casbah

I have a pre-formatted JSON blob stored as a string in MongoDB as a field in one of collections. Currently in my Scalatra based API, I have a before filter that renders all of my responses with a J...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.