Reputation: 171
Trying to run this ActiveRecord query, but running into a few errors. Looked at a few S.O. posts including this one but no luck still...
I have a model UserVote
and model User
. Here is the UserVote model
# UserVote.rb
belongs_to :voter, class_name: 'User', touch: true
belongs_to :voteable, class_name: 'User', touch: true
When there is a row in the DB, I can access the voter (User) object and voteable (User) object like so.
# Finding the voter's name
UserVote.find(13).voter.name
=> 'User1334342`
However, I am trying to query all of the UserVotes
where the voteable
object has a category
of Dancer
.
Something like this:
UserVote.joins(:voteable).where(["voteable.category = ?", "Dancer"]).count
This gives me the error:
SELECT COUNT(*) FROM "user_votes" INNER JOIN "users" ON "users"."id" = "user_votes"."voteable_id" WHERE (voteable.category = 'Dancer')
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "voteable"
Does anyone know what's going on here? Thanks for taking a look at it.
Upvotes: 0
Views: 62
Reputation: 26
"voteable" is the name of your relation, but the actual table name is "users". When you are specifying your where clause with a string like that, you are actually writing sql, so you are referencing a "voteable" table, which doesn't exist. Try this instead:
UserVote.joins(:voteable).where(["users.category = ?", "Dancer"]).count
Upvotes: 1