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_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

個人的に私は実際に飛行機や空港があり、次のようにこれらのレール団体を経由してDBを構築し、そこから行くでしょう:

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ルートを実行していて、他の:resource内のリソースをネストする場合は、すべてのレールヘルパーがその関係を使用するよりも優先されます。あなたはフォームを持っているようなものをいくつかする必要がありますform_for[OuterModelName, InnerModelName]。しかし、ほとんどの場合、Railsは詳細を処理します。

別々にアクセスできるようにするリソース(思考テーブル/モデル)については、空港とは独立してプレーンを更新したいと思う場合は、resources :planeを別のルートとして設定する必要があります(同様にネストされている)、つまりそのリソースへの参照が2つあります。

最後の注意として、それはあなただけの1つのインスタンス(showeditupdate、など)を使用しているとき、あなたはindexビュー(複数のレコード)とresourceがあるでしょうときresourcesです。

関連する問題