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

私ができるようにしたいのは、新しいトリップ レコードを作成し、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 レコードを作成するにはどうすればよいですか?

ありがとう!

4

1 に答える 1

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 ルートを実行していて、1 つの :resource を別の :resource 内にネストすると、すべての rails ヘルパーがその関係を使用するようになります。have forms do のように、いくつかのことを行う必要がありますform_for[OuterModelName, InnerModelName]。ただし、ほとんどの場合、Rails が詳細を処理します。

個別にアクセスできるようにしたいリソース(テーブル/モデルを考えてください)(たとえば、空港とは無関係に飛行機を更新したいと考えてください)の場合resources :plane、別のルートラインとして(およびつまり、そのリソースへの 2 つの参照があります。

最後にresourcesindexビュー (複数のレコード) がある場合とresource、1 つのインスタンス ( showeditupdateなど) を使用する場合です。

于 2012-02-09T03:48:29.350 に答える