0

I'm upgrading a project from Rails 3.2 to Rails 4.0, and I'm trying to fix a problem that is causing a deprecation warning. The warning is caused by a query like this one:

email = Email.
  includes(:person).
  where("lower(email) = ? ", login_email.downcase).
  first

Here the "emails" table has a string column "email" that holds the email address.

The deprecation warning is the Rails 4 one that says that since I am using where() with a SQL string and I am using includes() to eager-load, I should use references() to do a proper job of joining, so that rails doesn't have to parse the SQL to figure out the join. In this case, the SQL I am using references the model I am querying from, not an association, so there is no join involved with the SQL. To silence the warning, I was tempted to do the following bit of nonsense, saying that the join reference is the querying model:

email = Email.
  includes(:person).
  where("lower(email) = ? ", login_email.downcase).
  references(:emails).
  first

But this does not silence the deprecation warning.

The problem in a nutshell: I am calling where() with SQL that references the model I am querying from, I am using includes() to eager-load data from a table that is unrelated to the aforementioned SQL, but I am being asked to use references() in a situation where it doesn't apply.

Any tips on how to fix the problem that causes the deprecation warning in this case? The tip supplied by the warning itself doesn't really apply here.

Cheers, Chris

C. Want
  • 1
  • 1

1 Answers1

0

You can turn deprecation warnings off per environment... but I wouldn't recommend that (especially in development/test where they're useful warnings).

Take a gander at this discussion to see alternatives. Otherwise, I would advise to fix the deprecation warning (use the new syntax) and avoid muting the deprecations.

Community
  • 1
  • 1
daino3
  • 4,386
  • 37
  • 48
  • Thanks daino3. I would definitely prefer to fix the issue rather than silence the warning, I should modify my question to reflect that. – C. Want Feb 01 '16 at 21:53
  • OK, can you try `Email.includes(:person).where("lower(emails.email) = ?", login_email.downcase).first`... and if that doesn't work, throw in you `references(:emails)`. I think the SQL string thinks the `email` column is ambiguous amongst the two tables. – daino3 Feb 02 '16 at 16:04
  • Thanks again daino3. Unfortunately what you suggest doesn't stop the deprecation warning. I have found a way to silence the warning, by adding a ".joins(:person)" into the call chain. This is a bit of kludge to me, since while it is benign to do this in this case, it's not universally applicable, and I am only really doing it to silence a warning. – C. Want Feb 03 '16 at 15:46
  • Yeah, that definitely seems a bit kludgy. But oh well :/. Glad you figured it out and happy to help. – daino3 Feb 03 '16 at 18:16