0

I am trying to implement searching in my rails app. I have a User model where each user has an id and a facebook_id.

How can I implement searching by facebook_id? I was looking to call something like this: localhost:3000/users.json?facebook_id=123456.

Thanks!

Edit: I tried a conditional index, but received an error "We're sorry, but something went wrong."

This was my code:

  def index
    if params[:facebook_id]
      respond_with User.find(params:facebook_id)
    elsif params[:twitter_id]
      respond_with User.find(params:twitter_id)
    else
      respond_with User.all
    end
  end

Rails log:

2012-08-07T19:43:11+00:00 app[web.1]: Started GET "/users.json?facebook_id=1" for 173.247.200.7 at 2012-08-07 19:43:11 +0000
2012-08-07T19:43:11+00:00 app[web.1]: Processing by UsersController#index as JSON
2012-08-07T19:43:11+00:00 app[web.1]:   Parameters: {"facebook_id"=>"1"}
2012-08-07T19:43:11+00:00 app[web.1]: Completed 500 Internal Server Error in 2ms
2012-08-07T19:43:11+00:00 app[web.1]: 
2012-08-07T19:43:11+00:00 app[web.1]: NameError (undefined local variable or method `facebook_id' for #<UsersController:0x000000044b6e38>):
2012-08-07T19:43:11+00:00 app[web.1]:   app/controllers/users_controller.rb:4:in `index'

Edit2: After changing the params from params:facebook_id to params[:facebook_id] I received another error. Here is a copy of my rails log.

2012-08-07T19:53:45+00:00 app[web.1]: Started GET "/users.json?facebook_id=1000" for 173.247.200.7 at 2012-08-07 19:53:45 +0000
2012-08-07T19:53:45+00:00 app[web.1]: Processing by UsersController#index as JSON
2012-08-07T19:53:45+00:00 app[web.1]:   Parameters: {"facebook_id"=>"1000"}
2012-08-07T19:53:45+00:00 app[web.1]: Completed 500 Internal Server Error in 2ms
2012-08-07T19:53:45+00:00 app[web.1]: 
2012-08-07T19:53:45+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find User with id=1000):
2012-08-07T19:53:45+00:00 app[web.1]:   app/controllers/users_controller.rb:4:in `index'

3 Answers 3

6

You are accessing the params hash wrongly (you are doing params:facebook_id and it should be params[:facebook_id]). Try this:

def index
    if params[:facebook_id]
      respond_with User.find(params[:facebook_id])
    elsif params[:twitter_id]
      respond_with User.find(params[:twitter_id])
    else
      respond_with User.all
    end
  end

The second error (of your second edit) comes because you are trying to fetch a User that doesn't exist (id = 1000). This happens because you are looking for the User by its primary key (id). From the guides:

Model.find(primary_key) will raise an ActiveRecord::RecordNotFound exception if no matching record is found.

If you want to fetch by the facebook id, you should be able to do this:

User.find_by_facebook_id(params[:facebook_id])

This kind of finders are called Dynamic Finders which basically lets you do stuff like:

Model.find_by_field

Where field is any field or attribute of the table that is related to your Model.

Sign up to request clarification or add additional context in comments.

4 Comments

Woah, don't know how I missed the []! Give me one sec and I'll try it out.
Read the error. It's obvious what is happening, no? You are trying to fetch a User with Id = 1000, which doesn't exist.
It is searching for a User with id=1000, instead of with facebook_id=1000. Why?
Because you are doing User.find. Try doing User.find_by_facebook_id(params[:facebook_id])
1

Your approach is fine. You can add a query parameter as you suggested and in your controller add logic that will search the user if facebook_id is present. :facebook_id will be available in your controller action that matches the URL. In your case it is probably the User#index action.

Comments

0

You could put a search form in the View:

<%= form_tag user_path, :method => 'post' do %>
  <%= text_field_tag :search, params[:search] %>
  <%= submit_tag "Search", :name => nil %>
<% end %>

Something like the following in the Controller:

def index
  @users = User.search(params[:search])
end

And then in the Model:

def self.search(search)
  if search
    find(:all, :conditions => ['facebook_id LIKE ?', "%#{search}%"])
  else
  find(:all)
  end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.