我正在開發一款應用程序,用作飛機的日誌。我可能會以我的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時間(或者把它當作汽車上的當前里程數)。當我創建一個新旅程時,它將使用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
如果它更是一個嵌套表格的情況,我會如何獲取表單只是更新飛機的記錄,並創建一個新的旅行記錄?
謝謝!