开发者

Rails association fails on Heroku but works on local box?

开发者 https://www.devze.com 2023-02-23 03:55 出处:网络
Why would I get NoMethodError on my Heroku app when the same codeworks flawlessly on my local setup? The error is triggered by this code:

Why would I get NoMethodError on my Heroku app when the same code works flawlessly on my local setup?

The error is triggered by this code:

@customer = Customer.find(1)
@customer.responses.create(:offer_id => '1', :digit => '2')

That code works as int开发者_StackOverflow社区ended on my local server and in my local Rails console.

However, on Heroku the above code triggers NoMethodError:

NoMethodError (undefined method `responses' for #<Customer:0x7f7bcbee3808>):

The Response model is tied to the Customer model by means of belongs_to :customer

Additionally, I can login to the Heroku console and run this without any problems:

Response.create(:offer_id => '1', :customer_id => '1', :digit => '2')

So if the above works and both versions work fine on my local box, why would the association fail on Heroku?

--

Running Rails 3.0.6 and tested on Heroku Ruby 1.8.7 and Ruby 1.9.2

Databases are identical on Heroku and on my local box.


Usually when something like this doesn't work it indicates you're missing a has_many association. You need to define both the belongs_to and has_many sides of the association if you wish to access them both.

By the sounds of it, if it's working on your local machine but not Heroku then it would be because you haven't pushed the changes to the Heroku server and restarted the console there. Please make sure you have pushed the changes and try again.


Marco, I thought about this a bit, and I have a few guesses for you to try. Before you do either of these, restart your app. Sometimes that does miracles.

heroku restart

Ok, now, try in console just

@customer.responses

What does that return? I assume it should be []. Maybe doing some inspection etc. of that can give us insights here. If you build and associate a response manually can you get it to show up?

Second, your no method error is on responses, not on create, so whatever you type after that probably doesn't matter, BUT, are your offer_id and digit fields integers? If so, try creating them using integers, not strings. PostgreSQL is so fragile compared to MySQL or SQLite, I've had loads of issues that trace back to my unfamiliarity working with Postgre prior to developing on Heroku.

@customer.responses.create(:offer_id=>1,:digit=>3)

That probably doesn't matter but it's worth checking out.

The other thing to check is all your callbacks and validations etc. Is anything failing? It may not seem related but I've had issues before where things acted very weird because of seemingly tiny silent failures in a callback that I had overlooked. I'm sure you're testing as you go, but if you've got shallow test coverage anywhere on this model you might as well use this bug hunt as a chance to beef it up :)

Sympathies on the error, I don't know if any of this will help, but good luck! Please post if Heroku staff find the issue, I'd be very interested to learn from it!


I think you problem is how you are creating the response:

@customer.responses.create(:offer_id => '1', :digit => '2')

You might want to try this instead.

Response.create(:offer_id => '1', :digit => '2', :customer_id => 1)
0

精彩评论

暂无评论...
验证码 换一张
取 消