Ruby on rails 3.1 rails-3.1 |这是嵌套表单的情况还是其他形式的情况?
我正在开发一个应用程序,用作飞机日志。我的db设计可能会朝着错误的方向发展。关于这方面的任何建议都是有益的。这是嵌套表单的情况吗?或者,这只是在我需要同时为两个模型创建一个记录的情况下 到目前为止,我是这样布置模型的:Ruby on rails 3.1 rails-3.1 |这是嵌套表单的情况还是其他形式的情况?,ruby-on-rails-3.1,nested-forms,has-many-through,belongs-to,multi-model-forms,Ruby On Rails 3.1,Nested Forms,Has Many Through,Belongs To,Multi Model Forms,我正在开发一个应用程序,用作飞机日志。我的db设计可能会朝着错误的方向发展。关于这方面的任何建议都是有益的。这是嵌套表单的情况吗?或者,这只是在我需要同时为两个模型创建一个记录的情况下 到目前为止,我是这样布置模型的: class Location < ActiveRecord::Base has_many :aircrafts, :pilots end class Aircraft < ActiveRecord::Base belongs_to :location
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
如果它更像是一个嵌套的表单环境,我如何让表单只更新飞机记录并创建一个新的行程记录
谢谢 就个人而言,我实际上会拥有飞机和机场,并通过这些铁路协会按照如下方式构建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
class Airport:行程
结束
类Pilot:行程
结束
类Trip
我认为您参考的嵌套可能在设计路线时更为相关。例如,如果您正在执行RESTful路由,并且将一个:resource嵌套在另一个:resource中,那么您可以让所有rails助手使用该关系。您只需要为[OuterModelName,InnerModelName]做几件事,比如让表单执行
form\u。然而,在大多数情况下,Rails将处理细节
对于您确实希望能够单独访问的资源(想想表/模型)(想想,您想更新一个平面,独立于机场),那么您需要将资源:平面作为一条单独的路线(以及嵌套),即对该资源有两个引用
最后,当您只使用一个实例(show
,edit
,update
,等等)时,您将有一个索引
视图(多个记录)和资源
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