Description:
fixed bug: increament of number on old records in submission
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@98 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r46:c9ffe114dd77 - - 1 file changed: 3 inserted, 2 deleted
@@ -1,25 +1,25 | |||
|
1 | 1 | class Submission < ActiveRecord::Base |
|
2 | 2 | |
|
3 | 3 | belongs_to :language |
|
4 | 4 | belongs_to :problem |
|
5 | 5 | belongs_to :user |
|
6 | 6 | |
|
7 | 7 | validates_presence_of :source |
|
8 | 8 | validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long' |
|
9 | 9 | validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short' |
|
10 | 10 | validate :must_specify_language |
|
11 | 11 | validate :must_have_valid_problem |
|
12 | 12 | |
|
13 | - before_save :assign_latest_number | |
|
13 | + before_save :assign_latest_number_if_new_recond | |
|
14 | 14 | |
|
15 | 15 | def self.find_last_by_user_and_problem(user_id, problem_id) |
|
16 | 16 | last_sub = find(:first, |
|
17 | 17 | :conditions => {:user_id => user_id, |
|
18 | 18 | :problem_id => problem_id}, |
|
19 | 19 | :order => 'submitted_at DESC') |
|
20 | 20 | return last_sub |
|
21 | 21 | end |
|
22 | 22 | |
|
23 | 23 | def self.find_all_last_by_problem(problem_id) |
|
24 | 24 | # need to put in SQL command, maybe there's a better way |
|
25 | 25 | Submission.find_by_sql("SELECT * FROM submissions " + |
@@ -102,18 +102,19 | |||
|
102 | 102 | problem = Problem.find(self.problem_id) |
|
103 | 103 | else |
|
104 | 104 | problem = Submission.find_problem_in_source(self.source) |
|
105 | 105 | end |
|
106 | 106 | if problem==nil |
|
107 | 107 | errors.add_to_base("must specify problem") |
|
108 | 108 | elsif !problem.available |
|
109 | 109 | errors.add_to_base("must specify valid problem") |
|
110 | 110 | end |
|
111 | 111 | end |
|
112 | 112 | |
|
113 | 113 | # callbacks |
|
114 | - def assign_latest_number | |
|
114 | + def assign_latest_number_if_new_recond | |
|
115 | + return if !self.new_record? | |
|
115 | 116 | latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id) |
|
116 | 117 | self.number = (latest==nil) ? 1 : latest.number + 1; |
|
117 | 118 | end |
|
118 | 119 | |
|
119 | 120 | end |
You need to be logged in to leave comments.
Login now