首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails-3.1 |这是嵌套表单的案例还是其他表单的案例?

rails-3.1 |这是嵌套表单的案例还是其他表单的案例?
EN

Stack Overflow用户
提问于 2012-02-09 11:30:29
回答 1查看 64关注 0票数 1

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

到目前为止,我是这样布置模型的:

代码语言:javascript
复制
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列。使用Aircraft表,我基本上想要跟踪当前的hobbs时间(或者将其视为汽车的当前里程)。当我创建一个新的行程时,它将使用hobbs_out属性的当前霍布斯时间,并且hobbs_in将被记录在行程中并用于更新Aircraft.hobbs。

我能用trip_controller中的代码做到这一点吗?类似于:

代码语言:javascript
复制
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

如果这更像是一种嵌套表单环境,我如何才能让表单只更新飞机记录并创建新的行程记录?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-09 11:48:29

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

代码语言:javascript
复制
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中,那么您将获得所有的rails助手来使用该关系。你只需要做几件事,比如让表单做form_for[OuterModelName, InnerModelName]。然而,在大多数情况下,Rails将处理细节。

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

最后要注意的是,当您拥有一个index视图(多个记录)时,它是resources;当您只使用一个实例(showeditupdate等)时,它是resource

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9205021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档