ActiveRecord::UnknownAttributeError?

喜夏-厌秋 提交于 2019-12-10 01:34:32

问题


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.


回答1:


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.




回答2:


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.




回答3:


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

heroku restart --app staging

fixed this one for me.




回答4:


Try this

attr_reader :seats
attr_accessor :seats

instead of

attr_accessible :seats


来源:https://stackoverflow.com/questions/8139578/activerecordunknownattributeerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!