Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord::UnknownAttributeError?

I just pushed an app to a production Heroku environment.

Basically there is a Bus model and it has a seats attribute

class Bus < ActiveRecord::Base
  attr_accessible :seats, # other attributes
end

Now I have a JavaScript frontend which POST's JSON for new buses to the buses#create action.

ActiveRecord keeps encountering an error when I try to create a bus:

: POST www.busables.com/buses dyno=web.1 queue=0 wait=5ms service=65ms status=500 bytes=728
: 
: ActiveRecord::UnknownAttributeError (unknown attribute: seats):
:   app/controllers/buses_controller.rb:31:in `new'
:   app/controllers/buses_controller.rb:31:in `create'

The parameters are reaching the controller action fine. I can log them and I get the folowing:

The bus parameters received: {"seats"=>"24", "departure_time(1i)"=>"2011", "departure_time(2i)"=>"11", "departure_time(3i)"=>"25", "departure_time(4i)"=>"16", "departure_time(5i)"=>"15", "route_attributes"=>{"summary"=>"N51", "beginning_address"=>"A place", "terminal_address"=>"Another place", "distance"=>26362, "duration"=>1753}}

I checked that the Bus table actually has the seats column and it does (I ran this in the Heroku console):

> Bus.column_names
=> ["id", "name", "route_id", "created_at", "updated_at", "price", "departure_time", "trip_distance", "trip_duration", "seats"]

And of course I've tried migrating and loading the database schema. I've checked that the attr_accessible :seats is set correctly also.

Any other ideas?

I'm running Rails 3.1.1 on the Heroku Cedar stack. Everything works fine on my local machine.

like image 257
David Tuite Avatar asked Nov 15 '11 16:11

David Tuite


3 Answers

It's cliché but I tried again in the morning and it works perfectly! I suspect it might have been a propagation issue of some sort.

like image 80
David Tuite Avatar answered Oct 22 '22 08:10

David Tuite


I had this same issue with my Heroku app in production, but not with my nearly identical app in staging.

What was the difference? My staging app only had 1 web dyno instead of 2.

So I manually scaled my Production app down to 0 web dynos, then back up to 2.

BAM! Problem solved.

like image 45
dnszero Avatar answered Oct 22 '22 09:10

dnszero


Was pulling my hair out on this one until I saw Leito's comment above.

heroku restart --app staging

fixed this one for me.

like image 3
Bill Noto Avatar answered Oct 22 '22 09:10

Bill Noto