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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. On Jul 1, 9:37 pm, RailsFan Radha <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Habgood 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.-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
RailsFan Radha 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 July 2010 03:08, RailsFan Radha <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 9, 8:44 am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9 July 2010 03:08, RailsFan Radha <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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.For what it''s worth, reject{|q| q.nil?} is the same as compact Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Fri, Jul 9, 2010 at 05:28, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. - thanks, radha. Michael Pavling wrote:> On 9 July 2010 03:08, RailsFan Radha <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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.-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Fri, Jul 9, 2010 at 13:42, RailsFan Radha <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.