我正在构建一个Rails应用程序,其中我有一个机车和一个Trip模型。
每辆机车可以有多趟,每次旅行都属于一辆机车。Trip模型具有"begin“和"end”属性,它们是时间戳,可以为null。旅行“结束”是在机车到达目的地时设置一次;在此之前,它的值为空。
每当我想在特定机车的最后一次旅行中设置"end“属性(例如,id=45 ),麻烦就会开始。为此,我必须在所有旅行的集合中搜索"locomotive_id=45“上的匹配项,并搜索那些带有空"end”属性的旅行。只有这样我才能设定结束时间。
为了提高性能,我正在考虑在这两个模型中添加循环关联。在我的机车表中添加一个名为"last_trip_id“的列,它可以是null,它指向Trip表,并让我知道该表上的哪一行是该机车完成的最后一次旅行。
我认为这个想法棒极了!:D然而,我还没有在红宝石关联指南或它的API中找到任何关于循环关联的文档或提示。因此,我不知道在RoR框架上是否鼓励这种实现。
有人能给我一些关于这个问题的建议吗?是否鼓励增加这种循环联系,以便在这方面有更好的表现?
现在,我的机车和旅行模型看起来是这样的:
app/型号/机车。
has_many :trips, dependent: :restrict_with_errorapp/models/trip.rb
validates :locomotive_id, presence: true
belongs_to :locomotive我觉得我的新模特应该是这样的
app/型号/机车。
has_many :trips, dependent: :restrict_with_error
belongs_to tripapp/models/trip.rb
validates :locomotive_id, presence: true
belongs_to :locomotive
has_one :locomotive哪些方法会将rails添加到我的新模型中?我在写作时会遇到一些麻烦吗,例如:
> L=Locomotive.first
> L.trips.locomotives.trips.....诚挚的问候,
卡尔
编辑1这里有我的机车和trips结构:
db/schema.rb
...
create_table "locomotives", force: true do |t|
t.string "code"
t.datetime "created_at"
t.datetime "updated_at"
end
...
create_table "trips", force: true do |t|
t.integer "locomotive_id"
t.datetime "begin"
t.datetime "end"
t.datetime "created_at"
t.datetime "updated_at"
t.string "city_origin"
t.string "city_destination"
end发布于 2014-02-24 15:25:52
我将这样构造模式:请记住,使用您的模式,您正试图尽可能清楚地建模真实世界的概念,因此始终从真实世界的直观概念开始。
Station #list of stations
Locomotive #list of trains
#these are the **scheduled** journeys
Route
start_station_id
end_station_id
departure_time
arrival_time
#these are the **actual** journeys
Journey
route_id
locomotive_id
actual_departure_time
actual_arrival_time因此,车站、机车和线路都是预先存在的,然后通过火车为每一次真实的旅行创造一个旅程记录。route_id和locomotive_id的旅程是在预定的时完成的,而actual_departure_time和actual_arrival_time则是在火车实际离开和到达时填充的。
我认为这些类的名称可能会更好一些,而且会进一步分解,这样您就可以得到一个开始和结束站的表,还有另一个在不同时间有这些实例的表,但是您有希望得到这个想法。
发布于 2014-02-24 15:00:55
您可以引入一个时间表模型来容纳这些行程。这将允许您按日期范围对进出境的机车进行排序。
只是要小心你的模特旅行可能会“迟到”或“偏离”日程。
class CreateTrainManagementSystem < ActiveRecord::Migration
def change
create_table :locomotives do |t|
t.float :speed
end
create_table :schedules do |t|
t.integer :locomotive_id
t.integer :route_id
t.datetime :departure
t.datetime :arrival
end
create_table :routes do |t|
t.integer :origin
t.integer :destination
t.float :distance
end
create_table :trips do |t|
t.integer :schedule_id
t.datetime :departure
t.datetime :arrival
end
create_table :stations do |t|
t.string :title
t.string :address
t.string :city
t.string :state
t.integer :zip
t.decimal :coord_x
t.decimal :coord_y
end
end
end要拥有这个解决方案,您不能只是复制和粘贴它。这仍然需要模型、验证等等。它还解决了不仅仅是性能问题。考虑使用速度、行程日志和路线距离计算延迟列车的ETA。
@Karl,从你的评论中可以看出你不是学生就是刚毕业。这是个好消息,你在这里有很好的资源来问问题。如果你认为这是你的手艺,并想创造出像我这样的解决方案,请阅读Joe的“成批思考”一书。
发布于 2014-02-24 16:37:34
你的想法是有一个额外的领域从机车到最近的旅行,所以性能更好。如果您在trips表上设置了正确的索引,那么您可能不需要它。
尽管如此,如果你想做的话,下面是怎么做的。Rails具有将关联的名称设置为与类名相同的标准。但这不是法律。如果您的模型变得更加复杂,这通常不再是正确的事情。这样你就可以推翻它了。
你称这个协会为“最近的旅行”(或current_trip ?)告诉rails它实际上是一个Trip对象。
locomotive.rb
belongs_to :latest_trip,
class_name: "Trip",
foreign_key: "latest_trip_id"basics.html#belongs-to-association-reference
您需要向机车数据库表中添加一个字段"latest_trip_id“。您只需要在创建和对象时小心,所有字段都被正确设置。
你甚至可以有几个联想,如"funniest_trip","accident_trips",.
https://stackoverflow.com/questions/21990395
复制相似问题