我试图挽救ActiveRecords数组在我的控制器,但这个错误是显示出来:保存ActiveRecords对象的数组
未定义的方法“保存”为#
<Array:...>
我有模型的这种方法:
def self.import(file)
reservations = []
CSV.foreach(file, headers: true) do |row|
room_id = Classroom.where(code: row[0]).pluck(:id).first
row[0] = room_id
reservations << Reservation.new(number_of_guests: row[1], check_in_date: row[2], check_out_date: row[3], room_id: row[0])
end
reservations
end
而且我有这个以下控制器:
def create_import
@reservations = Reservation.import(params[:file].path)
respond_to do |format|
if @reservations.save
format.html { redirect_to @reservation, notice: 'Reservations was successfully created.' }
format.json { render :show, status: :created, location: @reservation }
else
format.html { render :import }
format.json { render json: @reservations.errors, status: :unprocessable_entity }
end
end
end
我该如何做这个保存方法?我想在我看来显示错误的报告。
如果'Classroom.where(code:row [0])。pluck(:id)'是'Classroom.where(code:row [0])。pluck(:id).first' ???第一个会给你包装一个数组的'room_id'。 – br3nt
@ br3nt是的,修复。谢谢 – gumaro