I want this SQL query to be written in rails controller using find
select id,name from questions where id not in (select question_id from levels_questions where level_id=15)
How will I do this?I am using Rails framework and MySQL db
thanks in advance.
views:
3247answers:
2
+3
A:
Question.find_all_by_sql('select id,name from questions where id not in (select question_id from levels_questions where level_id=15)')
This is admittedly non-ActiveRecord-ish, but I find that complicated queries such as this tend to be LESS clear/efficient when using the AR macros. If you already have the SQL constructed, you might as well use it.
Some suggestions: encapsulate this find call in a method INSIDE the Question class to hide the details from the controller/view, and consider other SQL constructions that may be more efficient (eg, an OUTER JOIN where levels_questions.question_id is null)
joshng
2009-06-12 06:04:36
of course. this is better.
Ryan Oberoi
2009-06-12 06:07:54
cool... first my answer was selected, then I find I have a -1 and I've been DEselected. and now the author of the selected answer says mine is better..? heh, ouch :-}
joshng
2009-06-12 06:27:15
well. I said yours was better when I saw it the first time, not after. I think your one-liner is superior. I gave you a +1 for your answer. I think you need to modify your answer to replace model.find_by_sql with Question.find_all_by_sql, then it will serve the author's purpose.
Ryan Oberoi
2009-06-12 06:31:26
thanks, ryan. I'm not bitter, it's just my REPUTATION at stake... :-D
joshng
2009-06-12 06:37:37
+4
A:
Simple way:
ids = LevelsQuestion.all(:select => "question_id",
:conditions => "level_id = 15").collect(&:question_id)
Question.all(:select => "id, name", :conditions => ["id not in (?)", ids])
One shot:
Question.all(:select => "id, name",
:conditions => ["id not in (select question_id from levels_questions where level_id=15)"])
Ryan Oberoi
2009-06-12 06:05:38
can this be given in the options_from_collection_for_select in the select_tag?
Nave
2009-06-12 06:12:16
I just edited it to change it from find to all. Select the version you like. I guess something made you change your mind. I like the other solution as well. Let us know why you selected my answer over the other one.
Ryan Oberoi
2009-06-12 06:20:29