1

我正在開發一款應用程序,用作飛機的日誌。我可能會以我的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 

如果它更是一個嵌套表格的情況,我會如何獲取表單只是更新飛機的記錄,並創建一個新的旅行記錄?

謝謝!

回答

0

我個人確實有飛機和機場,並通過這些軌道關聯結構分貝以下,然後從那裏:

class Airport < ActiveRecord::Base 
    has_many :planes 
    belongs_to :trip 
end 

class Plane < ActiveRecord::Base 
    belongs_to :Airport # Current location 
    has_many :trips 
    has_many :pilots, :through => :trips 
end 

class Pilot < ActiveRecord::Base 
    has_many :trips 
    has_many :planes, :through => :trips 
end 

class Trip < ActiveRecord::Base 
    belongs_to :plane 
    belongs_to :pilot 
    has_many :airports 
end 

我覺得嵌套您參考將在可能設計自己的路線更貼近。
例如,如果您正在執行RESTful路由,並且您在另一個資源中嵌套一個資源,則不會獲得所有使用該關係的路徑助手。你只需要做一些事情,比如有表格form_for[OuterModelName, InnerModelName]。然而,大多數情況下,Rails將處理細節。

對於您希望能夠分開訪問的資源(想想,您想要更新飛機,不管機場如何),那麼您需要將resources :plane作爲單獨的路線作爲單獨的路線(以及它嵌套),即將有兩個引用該資源。

最後要注意,當你只是用一個實例(showeditupdate等)的resources時,你就會有一個index視圖(多條記錄)和resource

相關問題