如何删除或销毁位于内存中但不在数据库中的对象?
irb(main):034:0> mentor.registered_students.build(:user_id => 20)
=> #<RegisteredStudent id: nil, user_id: 20, assigned_mentor_id: 1, description: nil, created_at: nil, updated_at: nil>
irb(main):035:0> mentor.registered_students.last
=> #<RegisteredStudent id: nil, user_id: 20, assigned_mentor_id: 1, description: nil, created_at: nil, updated_at: nil>
irb(main):036:0> mentor.registered_students.last.destroy
(0.3ms) BEGIN
(0.2ms) COMMIT
=> #<RegisteredStudent id: nil, user_id: 20, assigned_mentor_id: 1, description: nil, created_at: nil, updated_at: nil>
irb(main):037:0> mentor.registered_students.last.delete
=> #<RegisteredStudent id: nil, user_id: 20, assigned_mentor_id: 1, description: nil, created_at: nil, updated_at: nil>
irb(main):038:0> mentor.registered_students.last
=> #<RegisteredStudent id: nil, user_id: 20, assigned_mentor_id: 1, description: nil, created_at: nil, updated_at: nil>
我已经使用了销毁或删除,但他们在数据库中查找记录。
控制器动作:
def mix
unless params[:mentor_id].nil? || params[:students_id].nil?
@mentor = AssignedMentor.find params[:mentor_id]
@students = params[:students_id]
@students.each do |student|
if student[1] == "0"
registered_student = RegisteredStudent.where("assigned_mentor_id = ? AND user_id = ?", @mentor.id, student[0] ).first
registered_student.destroy
end
if student[1] == "1"
@mentor.registered_students.build(:user_id => student[0])
@mentor.save
if @mentor.errors.size > 0
@mentor.registered_students.reload
end
end
end
#redirect_to bind_admin_users_path
end
#flash[:alert] = t("labels.no_students")
redirect_to bind_admin_users_path
end