2012-02-28 3 views
0

활성 레코드 안에 데이터를 설정하는데 이상한 문제가 있습니다 .. 메소드 내부에서 데이터를 설정하려고해도 아무런 영향을 미치지 않습니다.레일스에서 ​​데이터 설정하기

여기에! 내 수업 클래스 '로

class Option < ActiveRecord::Base 
    serialize :returns_policy_refunds, Array 
    def reloadRefundOptions! 
    @returns_policy_refunds = WebSite.get_refund_options #options array 
    end 
end 

간단한입니까?

는 방법은을 설정 내가 .. BLOCK2 같이 BLOCK1에서 동일하게 볼 수 을 기대 .. 이제 .. 난 그냥 화면에 데이터를 뱉어있어

[email protected] do |option| 
    - option.returns_policy_refunds = ["wtf"] #just to reset things 

    <b>BLOCK 1</b> 
    = option.reloadRefundOptions! 
    = option.returns_policy_refunds 
    <br> 
    <b>BLOCK 2</b> 
    = option.returns_policy_refunds = WebSite.get_refund_options 
    = option.returns_policy_refunds 

를 직렬화를 테스트하려면 반환 정책 .. 첫 번째 옵션에서 실제로 볼 수있는 것 .returns_policy_refunds는 [ "wtf"]

무엇이 누락 되었습니까? 나는 틀린 일을해야 할 것이다. 그러나 나는 무엇을 모르고있다. 의견이 있으십니까?

답변

2

당신의 속성 지정에 @을 남겨주세요 :

class Option < ActiveRecord::Base 
    serialize :returns_policy_refunds, Array 
    def reloadRefundOptions! 
    self.returns_policy_refunds = WebSite.get_refund_options #options array 
    end 
end 

아직 시도하지 않은하지만 난 option.returns_policy_refunds이 속성의 데이터는 액티브에 의해 정의 된 해시 얻는 것을 말할 것입니다. @을 사용하여 클래스 변수를 지정하면 해당 클래스 변수가 방금 정의되어 있으며 특성 판독기 또는 직접 보내기를 사용하여 액세스 할 수 있습니다.

+0

기쁨이 아닙니다. 나는 그 b4를 시도했지만 지금도 시도했습니다. 나는 아직도 내 출력에서 ​​WTF를 본다. – baash05

+0

나는 내 코드를 http://tmonrails.wordpress.com/2009/08/05/ruby-setter-and-getter-methods/ – baash05

+0

에서 발견했다. 본인. 답입니다. – baash05

관련 문제