オブジェクト:保存するActiveRecordの配列が、私は私のコントローラでActiveRecordをの配列を保存しようとしているが、このエラーが現れてさ
未定義のメソッド#
<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
どうすればこの保存方法を行うことができますか?私は私の見解ではエラーのあるレポートを表示したい。
@reservations.each(&:save)
、または複数insert
機能を提供し、いくつかのサードパーティ製の宝石をインストールします。
「Classroom.where(code:row [0])。pluck(:id)」は「Classroom.where(code:row [0])。pluck(:id).first'ですか。最初のものは配列にラップされた 'room_id'を与えます。 – br3nt
@ br3ntええ、修正されました。ありがとうございます – gumaro