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!
Personally I would actually have Planes and Airports and structure the db as follows via these rails associations and then go from there:
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
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), ie there will be two references to that resource.
As a final note, it's resources
when you'll have an index
view (multiple records) and resource
when you are just using one instance ( show
, edit
, update
, etc).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.