私は、航空機のログブックとして使用するためのアプリケーションに取り組んでいます。私はdbの設計に間違った方向に向かっているかもしれません。それについての推奨事項は役に立ちます。これはネストされたフォームの状況ですか?それとも、実際に両方のモデルのレコードを同時に作成する必要がある場合のみですか?rails-3.1 |これはネストされたフォームのケースですか、別のケースのケースですか?
は今のように、これは私がモデルを打ち出してきた方法です:航空機レコード内ホッブズ列:
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
は、私は何をすることができるようにしたいことは、新たな旅の記録を作成し、更新することです。航空機のテーブルでは、基本的に現在のホッブズの時間を追跡したい(または、それを現在の走行距離と考える)。私が新しい旅行を作成するとき、それはhobbs_out属性のための現在のhobbs時間を使用し、旅行に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
は、それがどのように私は、航空機のレコードを更新し、新しいトリップレコードを作成するためのフォームになるだろう、ネストされたフォームの状況の詳細ですか?
ありがとうございます!