我試圖挽救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