Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Can't update data in model

Model ActvieRecrod Rollback

  • Please log in to reply
2 replies to this topic

#1 ryanmccoskrie

ryanmccoskrie

    Passenger

  • Members
  • 2 posts

Posted 12 September 2013 - 11:27 PM

I'm new to rails and and I can't find this in duckduckgo, ixquick or google.

 

I've got a model a model that each time I try updating data on it automatically rollsback.

 

The log of it is as follows:
 

  SQL (0.1ms)  BEGIN
  User Exists (0.3ms)  SELECT 1 AS one FROM `users` WHERE (`users`.`name` = BINARY 'ryanmccoskrie' AND `users`.`id` != 27) LIMIT 1
  User Exists (0.2ms)  SELECT 1 AS one FROM `users` WHERE (`users`.`email` = BINARY 'ryan.mccoskrie@gmail.com' AND `users`.`id` != 27) LIMIT 1
  User Exists (2.1ms)  SELECT 1 AS one FROM `users` WHERE (`users`.`ltcAddress` = BINARY 'witheld' AND `users`.`id` != 27) LIMIT 1
   (0.1ms)  ROLLBACK

The model file:

class User < ActiveRecord::Base
	has_secure_password
	
	validates :name, presence: true, uniqueness: true, length: { minimum: 3, maximum: 50}, format: { with: /\A[a-z][a-z0-9\-\_\.]+\z/ }
	validates :email, presence: true, uniqueness: true, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i }
	validates :password, presence: true, length: { minimum: 6 }
	validates :password_confirmation, presence: true, length: { minimum: 6 }
	
	has_many :offers, :through => :user_id
	
	before_save { |user| user.email = email.downcase }
	before_save :create_remember_token
	
	private
	def create_remember_token
		self.remember_token = SecureRandom.urlsafe_base64
	end
end

And the relevant passage in the schema:

create_table "users", force: true do |t|
    t.string   "name"
    t.string   "email"
    t.text     "postalAddress"
    t.string   "ltcAddress"
    t.string   "btcAddress"
    t.string   "password_digest"
    t.string   "remember_token"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Could someone explain whats going on? I don't have this issue with other models.



#2 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 12 September 2013 - 11:42 PM

One of your validates: condition is probably failing.  

 

My guess is your password: and password_confirm fields are blank? You're also using has_secure_password.  Password field blank, then password_digest will be blank too. 

 

But this is probably an existing record, and you don't want to change the password_digest, since you also don't know what the original password is. 

 

What to do?

 

You can use  .update_attribute so that validation isn't triggered.

@user.update_attribute(:your_field_name_here, some-value)  

 

If you have multiple fields to update, you'd have to do this for EACH field. 

 

You cannot use  .update_attributes  (<-- plural), because .update_attributes will also trigger the validations, which you don't want in this case. 



#3 ryanmccoskrie

ryanmccoskrie

    Passenger

  • Members
  • 2 posts

Posted 13 September 2013 - 02:54 AM

Thanks for the speady response. update_attribute does the trick.







Also tagged with one or more of these keywords: Model, ActvieRecrod, Rollback

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users