Query through a join table question

11 views
Skip to first unread message

Dave Castellano

unread,
Feb 27, 2014, 2:03:53 PM2/27/14
to rubyonra...@googlegroups.com
Hi,

I am trying to do the following database query (ie find all question
id's with the selected minisection id):

minisection_questions = Question.where(minisection_id:
minisection_id).pluck(:id)

I have a question table, a minisection table, and a
minisections_questions join table.

Question model - has_and_belongs_to_many :minisections
Minisection model - has_and_belongs_to_many :questions

I get the following error:
PGError: ERROR: column questions.minisection_id does not exist
LINE 1: SELECT id FROM "questions" WHERE "questions"."minisection_i...
^
: SELECT id FROM "questions" WHERE "questions"."minisection_id" = 2

--
Posted via http://www.ruby-forum.com/.

Ganesh Ranganathan

unread,
Feb 27, 2014, 3:01:13 PM2/27/14
to rubyonra...@googlegroups.com
Try Minisection.find(minisection_id).questions.pluck(:id)


--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3202d2b108b746f5280797b3d3c43a2e%40ruby-forum.com.
For more options, visit https://groups.google.com/groups/opt_out.

Dave Castellano

unread,
Feb 27, 2014, 3:30:43 PM2/27/14
to rubyonra...@googlegroups.com
Ganesh Ranganathan wrote in post #1138274:
> Try Minisection.find(minisection_id).questions.pluck(:id)

That did it! Thanks!!

Dave
Reply all
Reply to author
Forward
0 new messages