Rails从ActiveRecord对象生成错误的SQL

时间:2021-07-17 09:50:31

I have a simple ActiveRecord object representing an estimated time of arrival.

我有一个简单的ActiveRecord对象,表示估计的到达时间。

class Eta < ActiveRecord::Base
    belongs_to :stop
    belongs_to :vehicle
    validates :timestamp, :presence => true
end

In my database, I created the corresponding columns stop_id, vehicle_id, and timestamp (of type datetime).

在我的数据库中,我创建了相应的列stop_id、车载_id和时间戳(类型为datetime)。

I have a rake task set up to perform operations on this table, but it is generating SQL that doesn't make sense. If I try to run:

我有一个rake任务,用于在这个表上执行操作,但是它生成的SQL是没有意义的。如果我试着跑:

for eta in Eta.all
    puts eta.timestamp
end

It tries to SELECT * FROM eta, however the table is named etas, not eta.

它试图从eta中选择*,但是表被命名为etas,而不是eta。

The pluralized database naming is consistent with the rest of the tables created from ActiveRecord objects and I successfully created a similar ActiveRecord object that works correctly.

多元化的数据库命名与从ActiveRecord对象创建的其他表保持一致,我成功地创建了一个运行正常的类似ActiveRecord对象。

class PrecedingCoord < ActiveRecord::Base
    belongs_to :stop
    belongs_to :route
    belongs_to :coord
end

In the rake file:

在rake文件:

for eta in PrecedingCoord.all
    puts eta.coord.latitude
end

1 个解决方案

#1


6  

Rails sets the plural of ETA as ETA.

Rails将ETA的复数设置为ETA。

> 'eta'.pluralize
=> "eta"

If you have already created a table called etas, you can set this in your model:

如果您已经创建了一个名为etas的表,您可以在模型中设置它:

class Eta < ActiveRecord::Base
  set_table_name "etas"

  belongs_to :stop
  belongs_to :vehicle
  validates :timestamp, :presence => true
end

#1


6  

Rails sets the plural of ETA as ETA.

Rails将ETA的复数设置为ETA。

> 'eta'.pluralize
=> "eta"

If you have already created a table called etas, you can set this in your model:

如果您已经创建了一个名为etas的表,您可以在模型中设置它:

class Eta < ActiveRecord::Base
  set_table_name "etas"

  belongs_to :stop
  belongs_to :vehicle
  validates :timestamp, :presence => true
end