Hey all, This code does not upload the intended value to the database. I was expecting that if 2001 already exists as a student_number value for a record which has another column site 1 (represented by site_id), then we increment student_number by 1 so next value of student_number for that site would be 2002 and next value 2003 and so on. However, for some reason here, if 2001 exists, it just returns a value of 1, and then for next record created, it returns a value of 1 again and so on:
Student model:
def test!
update_attributes :updater_id => User.current_user.id,
:student_number =>
sitenum = self.site_id
count = Student.count_by_sql("SELECT MAX(student_number) FROM students WHERE site_id = #{sitenum}")
if count >= 2001
Student(:condition => { :site_id => sitenum },
:order => "student_number DESC").student_number + 1
else
2001
end
end
Students controller:
def test_finalize
if @student.update_attributes(params[:student]) && @student.test!
@student.save
end
end
Any idea of how to increment it by 1 if 2001 exists? Thanks for any suggestions.