航空機の航海日誌として使用するアプリに取り組んでいます。データベースの設計で間違った方向に向かっている可能性があります。それに関する推奨事項は役に立ちます。これはネストされたフォームの状況ですか? それとも、実際に両方のモデルのレコードを同時に作成する必要がある場合のみですか?
今のところ、これは私がモデルをレイアウトした方法です:
class Location < ActiveRecord::Base
has_many :aircrafts, :pilots
end
class Aircraft < ActiveRecord::Base
belongs_to :location
has_many :trips
end
class Pilot < ActiveRecord::Base
belongs_to :location
has_many :trips
end
class Trip < ActiveRecord::Base
belongs_to :aircraft, :pilot
end
ActiveRecord::Schema.define(:version => 20120209014016) do
create_table "aircrafts", :force => true do |t|
t.string "tail_number"
t.decimal "hobbs"
t.integer "location_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "aircrafts", ["location_id"], :name => "index_aircrafts_on_location_id"
create_table "locations", :force => true do |t|
t.string "city"
t.string "state"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "pilots", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.integer "location_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "pilots", ["location_id"], :name => "index_pilots_on_location_id"
create_table "trips", :force => true do |t|
t.date "date"
t.integer "aircraft_id"
t.decimal "hobbs_out"
t.decimal "hobbs_in"
t.integer "cycles_airframe"
t.integer "cycles_left"
t.integer "cycles_right"
t.time "time_out"
t.time "time_in"
t.integer "pic_id"
t.integer "sic_id"
t.string "flight_sequence"
t.text "remarks"
t.integer "miles"
t.datetime "created_at"
t.datetime "updated_at"
end
end
私ができるようにしたいのは、新しいトリップ レコードを作成し、Aircraft レコードの :hobbs 列を更新することです。Aircraft テーブルを使用して、基本的に現在の飛行時間を追跡したいと考えています (または、車の現在の走行距離と考えてください)。新しいトリップを作成すると、hobbs_out 属性に現在のホブス時間が使用され、hobbs_in がトリップに記録され、Aircraft.hobbs の更新に使用されます。
trip_controller のコードでこれを行うことはできますか? 何かのようなもの:
def create
@trip = Trip.new(params[:trip])
@aircraft = Aircraft.where(params[:trip][:aircraft_id])
@aircraft.hobbs = params[:trip][:hobbs_in]
@aircraft.save
respond_to do |format|
if @trip.save
format.html { redirect_to @trip, notice: 'Trip was successfully created.' }
format.json { render json: @trip, status: :created, location: @trip }
else
format.html { render action: "new" }
format.json { render json: @trip.errors, status: :unprocessable_entity }
end
end
end
ネストされたフォームの状況である場合、フォームで Aircraft レコードを更新して新しい Trip レコードを作成するにはどうすればよいですか?
ありがとう!