I have three models, Question, Response and User
User has many question, :through Responses
My response table has 3 main columns response, question_id and
user_id.
The use case is lets say the user can respond to 100 question, but
during a session he responds to 20. How do I find out the ones he has
not respondeded to. In this case the response table will have 20
records and I woukd like to show the user the rest of the 80 he has
not responded to.
Thanks
You could either use the find_by_sql method or the :joins method. (As
someone has stated earlier that avoid find_by_sql for performance and
maintanence reasons)
sample sql to put in find_by_sql in your model
select question_id from responses r1
where not exists
(select question_id from question q )
using :joins. (probably this would be better )
questions.find( :joins => “LEFT OUTER JOIN responses ON question_id =
responses.question_id” :conditions => “responses.id is null”)
gurus, please comment on this.
thanks,
radha.
badnaam wrote:
I have three models, Question, Response and User
User has many question, :through Responses
My response table has 3 main columns response, question_id and
user_id.
The use case is lets say the user can respond to 100 question, but
during a session he responds to 20. How do I find out the ones he has
not respondeded to. In this case the response table will have 20
records and I woukd like to show the user the rest of the 80 he has
not responded to.
Thanks
If your models are set up correctly you can do ‘@user.questions’ for
the collection. You can set up a method or named_scope maybe on the
user model:
def needed_questions
self.questions.reject{|q| q.nil?}.all
end
something like that.
I come from a db world.
Can someone interpret this please, “self.questions.reject{|q|
q.nil?}.all”
I’m overwhelmed by the syntax in the second line,
“self.questions.reject{|q| q.nil?}.all”
def needed_questions
self.questions.reject{|q| q.nil?}.all
end
something like that.
i can somewhat understand self.questions… but after that it is way
over my head.
I just completed reading “Simply Rails2” book and thought i could
understand… but this line is “self.questions.reject{|q| q.nil?}.all” is
complicated.
Please help.
thanks,
radha
Chris H. wrote:
If your models are set up correctly you can do ‘@user.questions’ for
the collection. You can set up a method or named_scope maybe on the
user model:
def needed_questions
self.questions.reject{|q| q.nil?}.all
end
something like that.
RailsFan R. wrote:
I come from a db world.
Can someone interpret this please, “self.questions.reject{|q|
q.nil?}.all”
I’m overwhelmed by the syntax in the second line,
“self.questions.reject{|q| q.nil?}.all”
Go look up the relevant functions (especially reject) in the
documentation. Also read the section on blocks in Programming Ruby or
other similar book. If you still have questions after reading those,
please ask. This is fairly basic Ruby stuff, and you need to know it to
be able to work with Rails.
On Jul 9, 8:44 am, Michael P. [email protected] wrote:
you just read the line out loud to yourself 
TBH, I think the “all” at the end is a syntax error - Array doesn’t
have a .all method, although I’m not at a Ruby console to check.
For what it’s worth, reject{|q| q.nil?} is the same as compact
Fred
On Fri, Jul 9, 2010 at 05:28, Frederick C.
[email protected] wrote:
For what it’s worth, reject{|q| q.nil?} is the same as compact
… if the collection is an Array. Without more context, I don’t know
if that’s so. The compact method is on that, not Enumerable.
-Dave
–
Specialization is for insects. | Professional: http://davearonson.com
-Robert Anson Heinlein | Programming: http://codosaur.us
-------------------------------+ Leadership: http://dare2xl.com
Have Pun, Will Babble! -me | Other: http://davearonson.net
Thanks michael. the link helps to understand .reject. thanks again
To get a clear pic, is the .all here truely a syntax error or typo or
is it correct?
“self.questions.reject{|q| q.nil?}.all”
can anyone clarify.
Michael P. wrote:
On 9 July 2010 03:08, RailsFan R. [email protected] wrote:
I come from a db world.
Can someone interpret this please, “self.questions.reject{|q|
q.nil?}.all”
this explains what “.reject” does:
module Enumerable - RDoc Documentation
So essentially, the line takes the collection of questions that self
has, and gets rid of those that are nil (fairly self-explanatory if
you just read the line out loud to yourself 
TBH, I think the “all” at the end is a syntax error - Array doesn’t
have a .all method, although I’m not at a Ruby console to check.
On 9 July 2010 03:08, RailsFan R. [email protected] wrote:
I come from a db world.
Can someone interpret this please, “self.questions.reject{|q|
q.nil?}.all”
this explains what “.reject” does:
http://ruby-doc.org/core/classes/Enumerable.html#M003126
So essentially, the line takes the collection of questions that self
has, and gets rid of those that are nil (fairly self-explanatory if
you just read the line out loud to yourself 
TBH, I think the “all” at the end is a syntax error - Array doesn’t
have a .all method, although I’m not at a Ruby console to check.
On Fri, Jul 9, 2010 at 13:42, RailsFan R. [email protected]
wrote:
To get a clear pic, is the .all here truely a syntax error or typo or
is it correct?
“self.questions.reject{|q| q.nil?}.all”
Again, without more context, I can’t tell, but it looks like an
ActiveRecord thing to me. It’s very common to have a
WhateverController’s index method do something like “@whatevers =
Whatever.all(:order => :name)”, when class Whatever inherits from
ActiveRecord::Base.
-Dave
–
Specialization is for insects. | Professional: http://davearonson.com
-Robert Anson Heinlein | Programming: http://codosaur.us
-------------------------------+ Leadership: http://dare2xl.com
Have Pun, Will Babble! -me | Other: http://davearonson.net