【问题标题】:rails-3.1 | Is this a case for a nested form or a case for something else?rails-3.1 |这是嵌套表单的情况还是其他情况?
【发布时间】:2012-03-01 13:53:35
【问题描述】:

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

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

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

如果更多的是嵌套表单的情况,我如何让表单只更新飞机记录并创建新的行程记录?

谢谢!

【问题讨论】:

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


    【解决方案1】:

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

    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 作为单独的路线as(以及嵌套),即对该资源有两个引用。

    最后一点,resources 当您将拥有 index 视图(多条记录)时,resource 当您仅使用一个实例时(showeditupdate、等等)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-08
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      相关资源
      最近更新 更多