Ruby on Rails Sunday, February 16, 2020

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?


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9a9006d2-b5c9-4e1a-a3ed-daa191524321%40googlegroups.com.

No comments:

Post a Comment