我正在运行Ruby on Rails 2.3.5和Ruby 1.8.7。
在生产模式下,代码执行两次出现问题。 Rails将数据保存在表中两次,以便创建冗余。
这是我的控制器:
def admission1
user = current_user
@student = Student.new(params[:student])
@selected_value = Configuration.default_country
@application_sms_enabled = SmsSetting.find_by_settings_key("ApplicationEnabled")
@last_admitted_student = Student.find(:last)
@config = Configuration.find_by_config_key('AdmissionNumberAutoIncrement')
@categories = StudentCategory.active
@batches = Batch.active
if request.post?
#add student multiple course
student_batch = params[:student][:batch_id]
if !student_batch.blank?
student_batch.each do |c|
batch = Batch.find c
@student.batches << batch unless @student.batches.include? batch
end
if @config.config_value.to_i == 1
@exist = Student.find_by_admission_no(params[:student][:admission_no])
if @exist.nil?
@status = @student.save
else
@last_admitted_student = Student.find(:last)
@student.admission_no = @last_admitted_student.admission_no.next
@status = @student.save
end
else
@status = @student.save
end
else
flash[:notice] = "#{t('insert_batch_record')}"
redirect_to :controller => "student", :action => "admission1", :id => @student.id
end
end
end
正如您所看到的,我正在尝试将学生模型保存在数据库中,但Rails正在将student_batches保存两次。这是日志:
Processing StudentController#admission1 (for 127.0.0.1 at 2015-11-09 21:56:06) [POST]
Parameters: {"action"=>"admission1", "student"=>{"email"=>"", "nationality_id"=>"77", "blood_group"=>"", "state"=>"", "address_line2"=>"", "address_line1"=>"", "student_category_id"=>"", "date_of_birth"=>"2010-11-09", "last_name"=>"", "admission_no"=>"0857", "middle_name"=>"", "gender"=>"m", "country_id"=>"77", "city"=>"", "birth_place"=>"", "batch_id"=>["2"], "first_name"=>"alitia", "phone2"=>"", "phone1"=>"", "pin_code"=>"", "religion"=>"", "admission_date"=>"2015-11-09"}, "controller"=>"student", "commit"=>"► Simpan & Lanjutkan", "authenticity_token"=>"p0NPObfIvU0IjS8NlfKOEfDT5m8I7L0Yt9y8RntjF3Y="}
[4;36;1mUser Columns (2.0ms)[0m [0;1mSHOW FIELDS FROM `users`[0m
[4;35;1mUser Load (0.0ms)[0m [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) [0m
[0m
[4;36;1mStudent Create (1.0ms)[0m [0;1mINSERT INTO `students` (`class_roll_no`, `is_deleted`, `created_at`, `student_batches_id`, `language`, `email`, `photo_data`, `user_id`, `ward_id`, `blood_group`, `nationality_id`, `is_sms_enabled`, `date_of_birth`, `student_category_id`, `address_line1`, `address_line2`, `state`, `updated_at`, `admission_no`, `last_name`, `is_active`, `has_paid_fees`, `middle_name`, `immediate_contact_id`, `photo_file_name`, `photo_file_size`, `gender`, `status_description`, `batch_id`, `birth_place`, `city`, `country_id`, `first_name`, `photo_content_type`, `admission_date`, `religion`, `pin_code`, `phone1`, `phone2`) VALUES(NULL, 0, '2015-11-09 14:56:06', NULL, NULL, '', NULL, 24, NULL, '', 77, 1, '2010-11-09', NULL, '', '', '', '2015-11-09 14:56:06', '0857', '', 1, 0, '', NULL, NULL, NULL, 'm', NULL, 1, '', '', 77, 'alitia', NULL, '2015-11-09', '', '', '', '')[0m
[4;35;1mStudentBatch Columns (2.0ms)[0m [0mSHOW FIELDS FROM `student_batches`[0m
#here is the problems, it create batch load batch and create another batch. It is supposed to insert the batch only once
[4;36;1mStudentBatch Create (0.0ms)[0m [0;1mINSERT INTO `student_batches` (`student_id`, `created_at`, `has_fees`, `updated_at`, `batch_id`) VALUES(16, '2015-11-09 14:56:06', 1, '2015-11-09 14:56:06', 2)[0m
[4;35;1mStudentBatch Load (0.0ms)[0m [0mSELECT * FROM `student_batches` WHERE (`student_batches`.student_id = 16) [0m
[4;36;1mStudentBatch Create (0.3ms)[0m [0;1mINSERT INTO `student_batches` (`student_id`, `created_at`, `has_fees`, `updated_at`, `batch_id`) VALUES(16, '2015-11-09 14:56:06', 1, '2015-11-09 14:56:06', 2)[0m
[paperclip] Saving attachments.
[4;35;1mSQL (79.6ms)[0m [0mCOMMIT[0m
[4;36;1mSmsSetting Load (1.0ms)[0m [0;1mSELECT * FROM `sms_settings` WHERE (`sms_settings`.`settings_key` = 'ApplicationEnabled') LIMIT 1[0m
Redirected to http://localhost:3000/student/admission1_2/16
Completed in 374ms (DB: 100) | 302 Found [http://localhost/student/admission1]
我截断了一些日志,因此您可以清楚地看到问题。在日志中,您可以看到Rails保存学生批次两次,而在代码中我只保存一次。
如果您需要型号,请告诉我。顺便说一下,我正在使用Fedena 2.3.5。
答案 0 :(得分:0)
student_batch.each do |c|
batch = Batch.find c
@student.batches << batch unless @student.batches.include? batch
end
将此更改为
batches=[]
student_batch.each do |c|
batch = Batch.find c
batches << batch unless @student.batches.include? batch
end
@student.batches=batches