views:

30

answers:

2
@user_post = Post.find(:all,:conditions => ["content LIKE ?", "%#{params[:query]}%"])

This particular statement returning nil even though there exists a record in the database. Where could be the problem?

Please help me out.

A: 

Have you verified that the parameters are being passed in correctly? When a query isn't returning expected results it is almost always because the query you think you are executing isn't really the query that you are executing. Take a look at your log files to verify the that the query is correct.

sosborn
Yes...I checked the development.log file...which is having the exact query as i expected...
vinkris
Processing SearchController#live_search (for 127.0.0.1 at 2010-09-30 22:52:15) [POST] Parameters: {"authenticity_token"=>"Uf0lChFYmx+k2yKlqTpU9umhAz3hVgHdO2CPmJU/JQE=", "query"=>"hi"} [4;36;1mUser Load (1.0ms)[0m [0;1mSELECT * FROM "users" WHERE ("users"."id" = 3) LIMIT 1[0m [4;35;1mPost Load (1.0ms)[0m [0mSELECT * FROM "posts" WHERE (content LIKE '%hi%') [0mRendering search/live_searchCompleted in 76ms (View: 19, DB: 2) | 200 OK [http://localhost/search/live_search]
vinkris
In that case there are only a couple of other possibilities. 1) the record that you think is in there really isn't. 2) It isn't really returning nil. How are you verifying that it is returning nil? Is it giving you problems in the view?
sosborn
Yes...in the view i am getting a nil value
vinkris
Can you show the code that are you using to show the view? Please include the controller action code as well.
sosborn
A: 

have you tried running resulting query in mysql console? if you do have result then keep debugging, error is somewhere between controller action and rendered view. maybe you're loading result in before_filter which is not executed for your action?

keymone