Stating the problem:
When trying to save my authentication that is related to my customer,
there is thrown a ActiveRecord:RecordNotSaved error!
My code:
Database:
table customers
table authentications
A Customer has_one :authentication
This is the look of my Customer model:
class Customer < ActiveRecord::Base
attr_accessible :id, :name, :email, :address_1, :address_2, :zipcode,
:city, :currency, :country_id, :contact_person_id
has_one :authentication
end
This is the look of my Authentication:
class Authentication < ActiveRecord::Base
attr_accessible :current_login_at, :customer_id, :failed_login_count,
:last_login_at, :login, :login_count, :password
validates :customer_id, :presence => true, :uniqueness => true
validates :password, :presence => true, :length => {:minimum => 5,
:maximum => 20}
validates_confirmation_of :password, :on => :update
belongs_to :customer
end
This is the look of my CustomersController:
#Make an authentication
authentication = Authentication.new
authentication.login = (@dbCustomer.id.to_s <<
@dbCustomer.zipcode.to_s).delete(’ ')
#Create random password, add it to the authentication
randPassword = RandomPasswordGenerator.generate(8,
:skip_symbols => true)
authentication.password = Password.create(randPassword)
#Add the counts
authentication.login_count = 0
authentication.failed_login_count = 0
#Add the customer ID
authentication.customer_id = @dbCustomer.id
if @dbCustomer.authentication = authentication ---> LINE 49
The exact error:
ActiveRecord::RecordNotSaved in CustomersController#create
Failed to save the new associated authentication.
Error in:
app/controllers/customers_controller.rb:49:in `create’