Note that there are some explanatory texts on larger screens.

plurals
  1. PORails - query works in view and controller in development, but only in view in production
    text
    copied!<p>I'm new to rails, so my question may have a simple answer. I am using rails 3.2.3. I have the following code to perform a search on my database:</p> <p><code>@search_results = User.search("test search")</code></p> <p>Where <code>search</code> is defined in my model. This code works perfectly for me when <code>@search_results = Data.search("test search")</code> is in either the view or controller in my development environment. In production, it works in the view, but not in the controller. I don't know why. Looking at my production log, there is a big difference in the SQL for the view and the controller:</p> <p>When query is from a view in production, this is what I see in the log:</p> <pre><code>SELECT 'users'.* FROM 'users' WHERE (concat(field1, ' ', field2, ' ', field3) LIKE '%test%' AND concat(field1, ' ', field2, ' ', field3) LIKE '%search%' </code></pre> <p>That is exactly what I want. However, when query is from controller, the SQL turns into this:</p> <pre><code>SELECT 'users'.* FROM 'users' WHERE (field1 LIKE '%test search%' OR field2 LIKE '%test search%' OR field3 LIKE '%test search%') </code></pre> <p>I don't understand what my controller in the production environment is doing to create this change.</p> <p>EDIT-Here is the <code>search</code> method:</p> <pre><code>def self.search(search) if search search_length = search.split.length find(:all, :conditions =&gt; [(["concat(field1, ' ', field2, ' ', field3) LIKE ?"] * search_length).join(' AND ')] + search.split.map { |name| "%#{name}%" }) else find(:all) end end </code></pre> <p>I am using MySQL on both production and development.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload