导轨-3.1 |这是嵌套表单的情况还是其他情况?

发布于 2025-01-03 11:27:18 字数 2569 浏览 1 评论 0原文

我正在开发一个用作飞机日志的应用程序。我的数据库设计可能走错了方向。任何有关这方面的建议都会有用。这是嵌套表单的情况吗?或者只有当我需要同时为两个模型创建记录时才这样做?

到目前为止,这就是我布置模型的方式:

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 时间作为 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

如果更多的是嵌套表单情况,我如何获取表单来更新飞机记录并创建新的旅行记录?

谢谢!

I am working on an app to be used as a logbook for aircraft. I may be headed in the wrong direction with design of my db. Any recommendations on that would be useful. Is this a situation for nested forms? Or is that only if I need to actually create a record for both models at the same time?

As of now this is how I have laid out the models:

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

What I want to be able to do is to create a new trip record and update the :hobbs column in the Aircraft record. With the Aircraft table I essentially want to track the current hobbs time (or think of it as the current mileage on a car). When I create a new trip it will use the current hobbs time for the hobbs_out attribute and the hobbs_in will be recorded in the trip and be used to update the Aircraft.hobbs.

Can I do this with code in the trip_controller? Something like:

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

If it is more of a nested form circumstance, how would I get the form to just update the Aircraft record and create a new Trip record?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朮生 2025-01-10 11:27:18

就我个人而言,我实际上会拥有飞机和机场,并通过这些铁路关联按如下方式构建数据库,然后从那里开始:

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 嵌套在另一个 :resource 中,那么您就可以让所有 Rails 帮助程序使用该关系。您只需要执行一些操作,例如让表单执行 form_for[OuterModelName, InnerModelName] 操作。然而,在大多数情况下,Rails 会处理细节。

对于您确实希望能够单独访问的资源(例如表格/模型)(例如,您想要更新飞机,独立于机场),那么您需要将 resources :plane 作为一个单独的路线(以及嵌套),即该资源将有两个引用。

最后一点,当您拥有 index 视图(多条记录)时,它是 resource;当您只使用一个实例时,它是 resource显示编辑更新等)。

Personally I would actually have Planes and Airports and structure the db as follows via these rails associations and then go from there:

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

I think the nesting you reference will be more relevant in designing your routes perhaps.
For instance if you are doing RESTful routes and you nest one :resource within another :resource than you get all the rails helpers to use that relationship. You just need to do a couple of things like have forms do form_for[OuterModelName, InnerModelName]. For the most part however Rails will handle the details.

For resources (think tables/models) that you do want to be able to access separately (think, you want to update a plane, independently of say an Airport) then you need to have resources :plane as a separate route line as(as well as having it nested), i.e. there will be two references to that resource.

As a final note, it's resources when you'll have an index view (multiple records) and resource when you are just using one instance (show, edit, update, etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文