Hey all, What if I want to check a column in a table to see if a value exists and if it doesn''t, then update the existing record with the specified value and if it does exist increment by 1 from the previous incrementation of the value. And do it using the model (or controller if it''s unable to be done in the model). Something like this: def test! update_attributes :student_number => if student_number.array.include?(2001) Student.student_number.first(:order => "student_number DESC").student_number + 1 else 2001 end end Can this be done? Now this immediately returns a nil.array error, but what I''m more concerned about is if it can search the table for a value and then if exists increment by previous incrementation. Thanks for any response. -- 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.
Philip Hallstrom
2010-Feb-04 19:21 UTC
Re: Checking column in table to see if value exists?
On Feb 4, 2010, at 10:34 AM, John Merlino wrote:> Hey all, > > What if I want to check a column in a table to see if a value exists > and > if it doesn''t, then update the existing record with the specified > value > and if it does exist increment by 1 from the previous incrementation > of > the value. And do it using the model (or controller if it''s unable > to be > done in the model). Something like this: > > def test! > update_attributes :student_number => > if student_number.array.include?(2001) > Student.student_number.first(:order => "student_number > DESC").student_number + 1 > else > 2001 > end > end > > Can this be done? > > Now this immediately returns a nil.array error, but what I''m more > concerned about is if it can search the table for a value and then if > exists increment by previous incrementation. Thanks for any response.Don''t do it this way. You''re going to run into collisions. What happens when two people run this method at the same time? I''m not entirely sure what you''re trying to do, but you should use a database sequence for this to ensure you don''t get duplicates. -philip -- 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.
Let me see if I got this right: The model name is "student" and the attribute is called "student_number". When a new student is created you want to make sure the student number has not been taken already, and if so, set it to the current maximum student number + 1. I cant make much sense of the code you posted but you could put the code for it in a filter on the student model: before_create(:check_student_number) private def check_student_number if(Student.find_by_student_number(self.student_number)) self.student_number = Student.maximum(:student_number) + 1 end def -- 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.
Marnen Laibow-Koser
2010-Feb-04 19:42 UTC
Re: Checking column in table to see if value exists?
John Merlino wrote:> Hey all, > > What if I want to check a column in a table to see if a value exists and > if it doesn''t, then update the existing record with the specified value > and if it does exist increment by 1 from the previous incrementation of > the value. And do it using the model (or controller if it''s unable to be > done in the model). Something like this: > > def test! > update_attributes :student_number => > if student_number.array.include?(2001) > Student.student_number.first(:order => "student_number > DESC").student_number + 1 > else > 2001 > end > end > > Can this be done?Yes, but you *really* don''t want to. Just use an autoincrement field in the DB (perhaps the id field). John, I''ve noticed that most of your posts here are on questions that could have been answered by a quick look at the Rails Guides or Programming Ruby. May I suggest that you spend less time posting and more time reading documentation? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.