In my controller a whole bunch of numbers are crunched. Percentages are multiplied by 100 so they can be stored as integers. Here is a partial list:
tot = @mot[1] + @mot[2] + @mot[3]
exec_pct = @mot[3] / tot * 100
tact_pct = @mot[2] / tot * 100
strat_pct = @mot[1] / tot * 100
Then the values are supposed to be written to the user record as follows:
current_user.update_attributes(:strat_pct => strat_pct.to_i, :tact_pct => tact_pct.to_i, :exec_pct => exec_pct.to_i )
The database has null values where the data should be stored.
Here's relevant portion of db schema:
t.integer "strat_pct"
t.integer "tact_pct"
t.integer "exec_pct"
UPDATE -
For testing purposes I have place eliminated calculation questions by inserting integers like so:
current_user.update_attributes(:strat_pct => 1, :tact_pct => 2, :exec_pct => 3 )
and:
p current_user.update_attributes(:strat_pct => 1, :tact_pct => 2, :exec_pct => 3 )
The fields are still null.
UPDATE 2 - User Model:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :login
validates :first_name, :last_name, :email, :password, :password_confirmation, :presence => true
before_create :create_login
def create_login
self.login = "#{last_name.capitalize}, #{fi开发者_如何学Crst_name.capitalize}"
end
has_many :answers
has_many :invitations
has_many :feedbacks
end
Thanks for your help.
There are a couple things that could be wrong.
First off, try:
p current_user.update_attributes(:strat_pct => strat_pct.to_i, :tact_pct => tact_pct.to_i, :exec_pct => exec_pct.to_i )
From the documentation:
If the saving fails because of a connection or remote service error, an exception will be raised. If saving fails because the resource is invalid then false will be returned.
The other thing that strikes me is that your logic will probably return 0 for everything:
tot = @mot[1] + @mot[2] + @mot[3] #assume [0,100,200,300] = 600
exec_pct = @mot[3] / tot * 100 #300 / 600000 (integers)= 0
tact_pct = @mot[2] / tot * 100 #200 / 600000 (integers)= 0
strat_pct = @mot[1] / tot * 100 #100 / 600000 (integers)= 0
Perhaps you mean:
tot = (@mot[1] + @mot[2] + @mot[3]).to_f #assume [0,100,200,300] = 600.0
exec_pct = (@mot[3] / tot) * 100 #300 / 600000.0 (float)= 50.0
tact_pct = (@mot[2] / tot) * 100 #200 / 600000.0 (float)= 33.333
strat_pct = (@mot[1] / tot) * 100 #100 / 600000.0 (float)= 16.666
In ruby, if you are doing float operations, at least one of the values must be a float. I am also assuming you are doing percentages, so I have added brackets around the division operation as that needs to be executed first.
Also, I could be wrong, but it looks like you are accidentally 1-indexing your array. Array indexes start from 0. If that is the case, you could change the code to:
@mot = [100,200,300]
total = @mot.inject(:+).to_f
@mot.map {|x| ((x/total.to_f)*100).to_i} #result => [16, 33, 50]
current_user.update_attributes(:strat_pct =>@mot[0], :tact_pct => t@mot[1], :exec_pct => @mot[2] )
EDIT
Your issue is here:
validates :first_name, :last_name, :email, :password, :password_confirmation, :presence => true
You are trying to update the attributes, but password and password_confirmation will be missing as it is not a real field. Change that line to:
validates :first_name, :last_name, :email, , :presence => true
validates :password, :password_confirmation, :presence => true, :on => :create
精彩评论