ruby-on-railsruby-on-rails-3searchfindbatman.js

Finding records by attribute value in a batman.js and rails 3 app


I have a User model in my app and I would like to "find" a User by the name attribute. I don't believe there is any out-of-the-box support for this in batman.js but it is possible that it is just undocumented. I will also need to make some changes to the Rails side to receive and process this type of request as well. Has anyone already tackled this type of problem?


Solution

  • I was mistaken, this functionality exists in Batman out-of-the-box.

    YourApp.User.load({name: "wuliwong"}, (err, results) ->
      #do stuff with the results array
    

    The UsersController#index action in Rails now looks like this:

    def index
      if params[:name]
        @users = User.where(:name => params[:name])
      else
        @users = User.all
      end
    end