I don't have a lot of experience with StackOverflow, but I'll try to help out here, anyway.
There's nothing obviously wrong with the code here, and trying out a new Rails app locally with this Payments model, I can't replicate the error you're getting. But there are a few unusual choices you're making here that I'm suspicious of, and any time you're having a hard-to-undesrtand error, it's helpful to try to narrow down the list of things that could be responsible.
Here are a couple of things I would want to straighten out before troubleshooting further:
* the first line of Payment requires "valid_email", but you don't say where that comes from, and since I don't have "valid_email", the require fails for me. it's possible (but unlikely) that it's adding something unexpected, since commenting that line out does make the Payment class work as expected for me. (It's also very likely that whatever "valid_email" does, it's not really necessary -- see
http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/ for more.)
* including ActiveModel::Validations is unnecessary here, since that module is already included into ActiveRecord, which Payments has as it's parent. I don't *think* it's causing the error you are seeing, but including it twice could have hard-to-predict consequences.
*
current_user.payments.new
isn't really what you want -- try
current_user.payments.build
instead. (As a general rule: you use "new" with upper-case classes, like Payment.new; use "build" with lower-case "associations", like user.payments.) You can read more about associations and "build" here:
http://guides.rubyonrails.org/association_basics.html
* @payment_current_user = current_user.payments.all
is not likely to be causing your error, but it's not really the best way to get the list of user payments. For one thing, it seems it's only used in the "new" view, but you're calling it in the "create" action because when "if save" fails, you wind up rendering the new action. If the user has many payments, this line will load all of them during the "create" action, but if the save is successful, the user will be redirected away and none of these payments would be needed -- meaning that you've exercised your database for no particularly good reason.
It's also not really necessary -- replace any instances of @payment_current_user in your view with "current_user.payments" and you'll be fine. As a
general rule, it's best if your controller only instantiates at most one "at-variable", focus on the data you need in the create action (the new payment) and let the view get the data it needs from @payment and current_user. (The exception(s) to "let the view get the data": at most one ".whatever" and don't call ".find" or ".where" from a view.)
One thing: what I'm doing here (adding your Payment model to a brand-new Rails app locally) is close, but not really replicating your setup over there -- you might have before_filters in your controllers or gems or plugins that add additional filters to ActiveRecord or who knows what lurking in your application that I can't predict. If you can't make your whole app available as a public repository on GitHub, you could at least post the complete controller in a gist (
https://gist.github.com).
If nothing else, putting the complete stack trace in a gist gives us some more information about the "layers" of code interacting to produce the error you see.