2013-10-20 1 views
0

나는 아이템 가격을 지정하는 메소드가 있습니다. 이 방법은 가격을 정확하게 계산하고보기에서 그 가격을 보여 주지만, 콘솔에 있으면 가격은 0으로 유지됩니다. 어떻게 가격을 정하면 저장되지 않습니까?레일 클래스 메소드가 뷰에 표시되어 있지만 속성을 설정하지 않습니다.

편집 : 가격이 큰 경우 큰 차이입니다.

def calc_price 
    self.price = 0 
    newprice = 0 
    pr = 0 

    pr = if amount < 10 
    product.pricerange0 
    elsif amount < 25 
    product.pricerange1 
    elsif amount < 50 
    product.pricerange2 
    elsif amount < 100 
    product.pricerange3 
    elsif amount < 250 
    product.pricerange4 
    elsif amount < 500 
    product.pricerange5 
    end 

    inkprice = (self.inkcolors*1.50) 
    newprice = ((pr+inkprice)*self.amount) 
    self.price = newprice 
    return self.price 
end 

콘솔에서 메소드를 실행하면 가격이 올바르게 지정되지만보기에서 할당되지 않습니다.

+0

관련없는,하지만 당신은 단지'홍보 =을 할 수없는 경우 양 <10; 0; elsif 금액 <25; product.pricerange1; elsif 양 <50; product.pricerange2 ...'등? 적어도 나를 읽는 것만으로는 읽기가 어렵습니다. 'if' 명령문의 반환 값을 사용하면 반복의 한 덩어리가 제거됩니다. 상한을 체크하면 다른 것을 제거 할뿐입니다. –

+0

@davenewton 감사합니다. 나는 더 좋은 길을 찾았지만 아직 하나도 알지 못했다. – user2769929

+0

세미콜론을 사용하지 않으려 고해서 주석에 개행을 지원하지 않습니다. –

답변

0

당신은 단지 값을 설정 한 후 저장 호출 할 필요가 :

def calc_price 
    self.price = 0 
    newprice = 0 
    pr = 0 

    pr = if amount < 10; 0; elsif amount < 25; 
    product.pricerange1; elsif amount < 50; 
    product.pricerange2; elsif amount < 100; 
    product.pricerange3; elsif amount < 250; 
    product.pricerange4; elsif amount < 500; 
    product.pricerange5; end 

    inkprice = (self.inkcolors*1.50) 
    newprice = ((pr+inkprice)*self.amount) 
    self.price = newprice # this just sets the value in this object 
    self.save    # this will persist the value in the database 
    return self.price 
end 
관련 문제