ActiveRecord::RecordNotSaved - Failed to save the new association Authentication


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’

On Oct 10, 2012, at 7:20 AM, Remi B. wrote:

Database:
attr_accessible :id, :name, :email, :address_1, :address_2, :zipcode,

#Make an authentication
authentication.login_count = 0

ActiveRecord::RecordNotSaved in CustomersController#create

Failed to save the new associated authentication.

Error in:

app/controllers/customers_controller.rb:49:in `create’

What does this line say? (We don’t see line numbers in the code you
posted here.) What happens if you change save to save! near here, so
that any errors are flagged immediately? An invalid record will not
save, so what are the errors on that object?

Walter

Line 49 is the last line given in my CustomersController code. (—>LINE
49)

The problem is that there isn’t a save, but I am just connecting the
authentication to my customers authentication association.

I can’t do the save! because of that…

Ah, then you probably missed an end somewhere.

Walter

On Oct 11, 2012, at 2:03 PM, Remi B. wrote:


match

Please sign up to access the customer portal


  • <% if @customer.errors.any? %>

    <%= c.text_field :email%> --------------------- The exact error: ---------------------

    THERE IS NO ERROR (and that is the problem)

  • What’s in your model? Do you have validates_confirmation_of :email in
    there somewhere?

    Walter

    Sorry, did not include that one…

    validates :email, :presence => true, :confirmation => true, :format =>
    {:with => /\A[A-Za-z0-9._%±][email protected][A-Za-z0-9.-]+.[A-Za-z]+\z/}

    Thats what is in my Customer validation

    Solution

    Because of the hashing of my password, it validated the wrong length.
    Now I made sure that my validation was performed before I hashed my
    password.

    NEW PROBLEM (damn it’s still tricky for me)


    Stating the problem:

    Trying to validate my email with a conformation, but not working at all
    (but if I do save! it sends me the :

    Validation failed: Email doesn’t match confirmation

    What I am expecting. But unfortunately I don’t get the error in my
    form…


    My code:

    CustomersController


    @dbCustomer.email = @customer.email
    @dbCustomer.email_confirmation = @customer.email_confirmation

    unless @dbCustomer.save #on save! -> Validation failed. Email doesn’t
    match
    render :action => :edit, :notice => “Hello!”
    return
    end

    edit.html.erb


    <% if @customer && @customer.is_signup == false %>

    Please submit your personal password:

    <%= render 'firstLoginForm'%> <% else @customer = Customer.new %>

    Please sign up to access the customer portal

    <%= render 'signUpForm'%> <% end %>

    _firstLoginForm


    <%= form_for @customer do |f| %>
    <% if @customer.errors.any? %>



      <% @customer.errors.full_messages.each do |msg| %>

    • <%= msg %>

    • <%end%>
      </ul>
    </div>
    

    <% end %>
    <%= fields_for :authentication do |a| %>
    <%= a.label :password %>
    <%= a.password_field :password %>


    <%= a.label :password_confirmation%>
    <%= a.password_field :password_confirmation %>
    <% end %>


    <%= f.submit “Save your changes”%>
    <% end %>

    _signUpForm.html.erb


    <%= form_for @customer do |c|%>
    <% if @customer.errors.any? %>



      <% @customer.errors.full_messages.each do |msg| %>

    • <%= msg %>

    • <%end%>


    <% end %>


    <%= c.label :id%>
    <%= c.text_field :id%>



    <%= c.label :name %>
    <%= c.text_field :name%>


    Please ad a valid e-mail to receive your credentials:



    <%= c.label :email%>
    <%= c.text_field :email%>



    <%= c.label :email_confirmation %>
    <%= c.text_field :email_confirmation %>



    <%= c.submit%>


    <% end %>

    The exact error:

    THERE IS NO ERROR (and that is the problem)

    Greetings (and a lot of thanks!)

    This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

    | Privacy Policy | Terms of Service | Remote Ruby Jobs