Austin Wagner
Austin Wagner

Reputation: 1081

Rails generating incorrect SQL from ActiveRecord object

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

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).

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:

for eta in Eta.all
    puts eta.timestamp
end

It tries to SELECT * FROM eta, however the table is named etas, not 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.

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

In the rake file:

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

Upvotes: 2

Views: 184

Answers (1)

Douglas F Shearer
Douglas F Shearer

Reputation: 26528

Rails sets the plural of ETA as ETA.

> 'eta'.pluralize
=> "eta"

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

class Eta < ActiveRecord::Base
  set_table_name "etas"

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

Upvotes: 6

Related Questions