ActiveRecord Error: Where clause hash conditions with table prefix

68 views
Skip to first unread message

Hélio Pereira

unread,
Feb 16, 2020, 9:06:53 AM2/16/20
to Ruby on Rails: Talk
Hi everyone,

Before submitting and issue in rails github I'm asking if this is a normal behaviour in rails activerecord. 

Without table prefix in comments table the following code prints the correct SQL Syntax:

Post.distinct.joins(:comments).where(comments: { user: current_user }).to_sql

SELECT DISTINCT posts
.*
FROM
 posts INNER JOIN comments
 ON comments
.post_id = posts.id
WHERE
 comments
.user_id = 1


With table prefix in comments table the following code prints the wrong SQL Syntax:

Post.distinct.joins(:comments).where(prefix_comments: { user: current_user }).to_sql

SELECT DISTINCT posts
.*
FROM
 posts INNER JOIN prefix_comments
 ON prefix_comments
.post_id = posts.id
WHERE
 prefix_comments
.user = 1

It seems that when using table prefixes rails does not convert the hash condition for the model user to the correct column user_id and of course throws an error.

In rails doc it says that:

author = Author.find(1)

# The following queries will be equivalent:
Post.where(author: author)
Post.where(author_id: author)

And its going to get even worst for polymorphic tables.

Is this excepted or is this a bug in rails and I should open an issue for that?


Reply all
Reply to author
Forward
0 new messages