I’m building an autocomplete feature for my rails app. The input form makes an ajax call on keypress which then calls this search method in rails
def search
search = Regexp.new(params[:name])
@users = User.where(:name=>search)
render :json => @users
end
All works fine… However, the user has a bunch of fields that are foreign keys to other collections.
For example, one of these fields is for rolify. role_ids which looks like "role_ids" : [ObjectId("508eee5afa0d00b818000001"), ObjectId("508eee92fa0d00b818000002")] which each map to something like administrator or moderator
What I want to do is replace these ids with the actual document from the roles collection. I know I can just manually fill it out, but I was hoping that there would be some way with Mongoid to automatically populate.
Haven’t used Mongoid myself, but with ActiveRecord you would do it like this:
This of course needs your
Usermodel to haverolesassociation set.Hope this is relevant in Mongoid too.