1

I've populated a hash with two different models. I then try to sort them like so:

@search_results = User.find(:all, :conditions => ['name LIKE ?', "%#{params[:query]}%"])
@search_results += Book.find(:all, :conditions => ['title LIKE ?', "%#{params[:query]}%"])
@search_results.sort! { |a,b| a.impressions_count <=> b.impressions_count }

This throws the following error:

comparison of User with Book failed

Both users and books have an integer-based impressions_count. Why can't I sort via this attribute? What other options do I have?

1 Answer 1

1

I faced a similar problem recently and ended up writing some custom sql because all other ways returned an array. Pretty sure its not a good idea to use the sort method since it will always be more efficient to sort in SQL than ruby, especially when the data set gets large

  @combined_results = User.find_by_sql(["SELECT title, id, impressions_count, NULL as some_attribute_of_book
                                         FROM user
                                         WHERE title LIKE ?
                                         UNION SELECT title, id, impressions_count, some_attribute_of_book FROM book 
                                         WHERE title LIKE ?
                                         ORDER BY impressions_count", params[:query], params[:query]])

The above is completely untested code, more of an example than anything

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

8 Comments

After tinkering with this some, I almost have it working. (I hope.) The only problem is that its passing nil values. What might be causing this? I suspect it has something to do with "some_attribute_of_book," though I'm not sure what to put in its place.
some_attribute_of_book was just an example of how you could retrieve a field as NULL if it was not in the other model. Since you must have a field in both tables for a union to work. So if you dont need anything like that you can just remove it, otherwise replace with any field you want to pull back from the books table or vice versa. Hope that helps
I'm no longer getting nil results, but the hash is nevertheless empty. Even if I trim it down to this: User.find_by_sql(["SELECT name FROM users WHERE name LIKE ?", params[:query]])
Ah, now I see what's going on. Typically I can get the results by passing in just a portion of the query, such as "ook" returning "The Book of the Dead" and "George Book." Now I have enter the full search query for it work. I'm not sure why this is happening, as I'm still using LIKE. I'll look into this and let you know what I find.
Got it. I needed to replace params[:query] with "%#{params[:query]}%". Anyway, it's now working great. Thanks for your help, Jon!
|

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.