Remi B.
2012-Oct-10 11:20 UTC
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''
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Walter Lee Davis
2012-Oct-10 12:54 UTC
Re: ActiveRecord::RecordNotSaved - Failed to save the new association Authentication
On Oct 10, 2012, at 7:20 AM, Remi B. wrote:> -------------------- > 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''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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Remi B.
2012-Oct-11 14:23 UTC
Re: ActiveRecord::RecordNotSaved - Failed to save the new association Authentication
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... -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Walter Lee Davis
2012-Oct-11 14:47 UTC
Re: ActiveRecord::RecordNotSaved - Failed to save the new association Authentication
Ah, then you probably missed an end somewhere. Walter On Oct 11, 2012, at 10:23 AM, Remi B. wrote:> 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... > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Remi B.
2012-Oct-11 18:03 UTC
Re: ActiveRecord::RecordNotSaved - Failed to save the new association Authentication
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 %>
<h2>Please submit your personal password:</h2>
<%= render ''firstLoginForm''%>
<% else @customer = Customer.new %>
<h2>Please sign up to access the customer portal</h2>
<%= render ''signUpForm''%>
<% end %>
_firstLoginForm
***************
<%= form_for @customer do |f| %>
<% if @customer.errors.any? %>
<div class="form_errors">
<ul>
<% @customer.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<%end%>
</ul>
</div>
<% end %>
<%= fields_for :authentication do |a| %>
<%= a.label :password %>
<%= a.password_field :password %>
<br />
<%= a.label :password_confirmation%>
<%= a.password_field :password_confirmation %>
<% end %>
<br />
<%= f.submit "Save your changes"%>
<% end %>
_signUpForm.html.erb
********************
<%= form_for @customer do |c|%>
<% if @customer.errors.any? %>
<div id="form_errors">
<ul>
<% @customer.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<%end%>
</ul>
</div>
<% end %>
<p>
<%= c.label :id%>
<%= c.text_field :id%>
</p>
<p>
<%= c.label :name %>
<%= c.text_field :name%>
</p>
<h3>Please ad a valid e-mail to receive your credentials:</h3>
<p>
<%= c.label :email%>
<%= c.text_field :email%>
</p>
<p>
<%= c.label :email_confirmation %>
<%= c.text_field :email_confirmation %>
</p>
<p class="button">
<%= c.submit%>
</p>
<% end %>
---------------------
The exact error:
---------------------
THERE IS NO ERROR (and that is the problem)
Greetings (and a lot of thanks!)
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Walter Lee Davis
2012-Oct-11 18:27 UTC
Re: ActiveRecord::RecordNotSaved - Failed to save the new association Authentication
On Oct 11, 2012, at 2:03 PM, Remi B. wrote:> 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 %> > <h2>Please submit your personal password:</h2> > <%= render ''firstLoginForm''%> > <% else @customer = Customer.new %> > <h2>Please sign up to access the customer portal</h2> > <%= render ''signUpForm''%> > <% end %> > > _firstLoginForm > *************** > <%= form_for @customer do |f| %> > <% if @customer.errors.any? %> > <div class="form_errors"> > <ul> > <% @customer.errors.full_messages.each do |msg| %> > <li> > <%= msg %> > </li> > <%end%> > > </ul> > </div> > <% end %> > <%= fields_for :authentication do |a| %> > <%= a.label :password %> > <%= a.password_field :password %> > <br /> > <%= a.label :password_confirmation%> > <%= a.password_field :password_confirmation %> > <% end %> > <br /> > <%= f.submit "Save your changes"%> > <% end %> > > _signUpForm.html.erb > ******************** > <%= form_for @customer do |c|%> > <% if @customer.errors.any? %> > <div id="form_errors"> > <ul> > <% @customer.errors.full_messages.each do |msg| %> > <li> > <%= msg %> > </li> > <%end%> > </ul> > </div> > <% end %> > <p> > <%= c.label :id%> > <%= c.text_field :id%> > </p> > <p> > <%= c.label :name %> > <%= c.text_field :name%> > </p> > <h3>Please ad a valid e-mail to receive your credentials:</h3> > <p> > <%= c.label :email%> > <%= c.text_field :email%> > </p> > <p> > <%= c.label :email_confirmation %> > <%= c.text_field :email_confirmation %> > </p> > <p class="button"> > <%= c.submit%> > </p> > <% end %> > > --------------------- > 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> > > Greetings (and a lot of thanks!) > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Remi B.
2012-Oct-11 21:31 UTC
Re: ActiveRecord::RecordNotSaved - Failed to save the new association Authentication
Sorry, did not include that one...
validates :email, :presence => true, :confirmation => true, :format
=>
{:with => /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/}
Thats what is in my Customer validation
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.