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

时间:2012-02-09 03:30:29

标签: ruby-on-rails-3.1 nested-forms has-many-through belongs-to multi-model-forms

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

截至目前,这就是我制作模型的方式:

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_out属性的当前hobbs时间,并且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

如果它更像是一种嵌套形式的情况,我如何才能让表格更新飞机记录并创建新的旅行记录?

谢谢!

1 个答案:

答案 0 :(得分: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路由并且您将一个资源嵌套在另一个:资源中,而不是让所有rails帮助程序使用该关系。你只需要做一些事情,比如表格form_for[OuterModelName, InnerModelName]。但在大多数情况下,Rails将处理细节。

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

最后一点,当您使用一个实例(resources时,index视图(多个记录)和resource时,showeditupdate,{{1}}等)。