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.
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")
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.
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.
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.
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.