导轨-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 时间作为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我个人而言,我实际上会拥有飞机和机场,并通过这些铁路关联按如下方式构建数据库,然后从那里开始:
我认为您参考的嵌套可能在设计您的路线时更相关。
例如,如果您正在执行 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:
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 anindex
view (multiple records) andresource
when you are just using one instance (show
,edit
,update
, etc).