Ruby On Rails循环关联定义



我正在构建一个RubyonRails应用程序,其中有一个机车和一个Trip模型。

每台机车可以有多个行程,每个行程属于一台机车。Trip模型有一个"begin"one_answers"end"属性,它们是时间戳,可以为空。一旦机车到达目的地,则设置行程"结束";在此之前,其"值为null。

每当我想为特定机车(例如id=45)设置最后一次行程的"结束"属性时,故障就会开始。为此,我必须在所有行程的集合中搜索"motive_id=45"上的匹配项,并搜索那些具有null"end"属性的行程。只有到那时,我才能设定结束时间。

为了提高性能,我正在考虑为这两个模型添加循环关联。在我的机车表中添加一个名为"last_trip_id"的列,它可以为null,指向trip表,让我知道该表上的哪一行是该机车完成的最后一次行程。

我觉得这个主意太棒了!:D然而,我还没有在ruby关联指南或它的API中找到任何关于循环关联的文档或提示。。。所以我不知道RoR框架是否鼓励这种实现。

有人能给我一些关于这个问题的建议吗?是否鼓励增加这种循环关联,以便在这种情况下有更好的表现?

现在,我的机车和旅行模型是这样的:

app/models/motive.rb

has_many :trips, dependent: :restrict_with_error

app/models/trip.rb

validates :locomotive_id, presence: true
belongs_to :locomotive

我认为我的新机型应该是这样的:

app/models/motive.rb

has_many :trips, dependent: :restrict_with_error
belongs_to trip

app/models/trip.rb

validates :locomotive_id, presence: true
belongs_to :locomotive
has_one :locomotive

哪些方法将为我的新模型添加轨道?我在写作时会遇到一些麻烦吗,例如:

> L=Locomotive.first
> L.trips.locomotives.trips.....

致问候,

Karl

第1版这是我的机车和行程结构:

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

我会这样构建模式:记住,使用你的模式,你试图尽可能清晰地建模真实世界的概念,所以总是从真实世界的直观概念开始。

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和motive_id在行程计划时填写,actual_departure_time和actual_arrival_time在列车实际出发和到达时填写。

我认为类的名称可以更好一点,Journey可以被进一步分解,这样你就有了一个start&终点站,还有一个在不同时间有这样的例子,但你很有希望。

您的想法是从机车到最近一次旅行有一个额外的字段,这样性能会更好。如果你在旅行表上设置了正确的索引,也许你不需要它

尽管如此,如果你想做到这一点,以下是如何做到的。Rails的标准是将关联的名称设置为与类名相同。但这不是法律。如果你的模型变得更加复杂,这通常不再是正确的做法。所以你可以覆盖它。

您将关联称为"最新行程"(或current_trip?),并告诉rails它实际上是一个trip对象。

motive.rb

belongs_to :latest_trip, 
class_name: "Trip",
foreign_key: "latest_trip_id"

http://guides.rubyonrails.org/association_basics.html#belongs-到关联参考

您需要向机车数据库表中添加一个字段"latest_trip_id"。您只需要在创建和对象时小心,确保所有字段都已正确设置。

你甚至可以与Trip有几个关联,比如"有趣的_Trip"、"意外的_trips",。。。。

您可以引入一个时间表模型来保存这些行程。这将允许您按到达/离开机车的日期范围进行排序。

只要注意你的模特之旅是否会"迟到"或"偏离"时间表。

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 Celko的《成套思考》一书。

最新更新