2013-10-14 2 views
0

모델 레벨에 TeacherPayslip으로 속성을 업데이트하는 코드가 있습니다.레일에서 Update_attribute가 작동하지 않습니다. 4

TeacherPayslip.rb (모델) 내가 9789. 좋아하지만 업데이트 한 후 @netsalary 쇼를 올릴 때

다음
def net_salary 
    @teacher_id = self.id 
    @da = (self.basic * self.da)/100 
    @hra = (self.basic * self.hra)/100 
    @gs = @da + @hra + self.basic 
    @pf = (@gs * self.pf)/100 
    @netsalary = @gs - @pf + self.special_allowance + self.bonus 

    @a = TeacherPayslip.find(@teacher_id) 
    #raise @a.inspect 

    raise @a.update_attribute('net_salary',@netsalary).inspect 
    end 

(@ a.update_attribute가) netsalary @ ('net_salary'net_salary가 진정한 가치를 보여줍니다. 대신 9789.

+1

'update_attribute' 방법은 참 또는 거짓뿐만 아니라 값 자체를 반환합니다. 그게 사법인가? – tihom

+0

값이 – user2310209

답변

-1

update_attribute 반환 부울 ​​(true 또는 false)의. 옆에, 콜백 해고하지 않는 원인이됩니다 update_attribute 대신 update_attributes의 사용.

당신은 사용해야

@a.update_attributes(net_salary: @netsalary) 
@a.net_salary # will hold the new value 
+0

대신 true로 표시됩니다. 실제 값이 – user2310209

+0

인데도 불구하고 실제로는 'update_attribute'가 콜백을 실행하지만 유효성 검사를 실행하지 않습니다. –

+0

@MarekLipka true이면 유효성 검사 콜백이 실행되지 않습니다. –

관련 문제