2010-07-01 4 views
0

Ruby에서 수행 한 프로젝트에 기능을 추가하려고하는데 Ruby에 익숙하지 않지만 프로젝트 리뷰 페이지를 작성하여 주어진 월별 검토를위한 프로젝트 작업 코드Ruby attr_accessor setter가 하위 객체의 update_attributes 중에 호출되지 않습니다.

제 문제는 클라이언트 (내 동생)가 "이번"달의 프로젝트 검토에서 다음 몇 달 동안 예정된 시간을 편집하도록 허용했음을 말합니다.

페이지에서 자식에 속하지 않은 값을 표시 할 수 있었고 일반적인 자식 요소를 업데이트 할 수는 있지만 값이 업데이트되도록 할 수는 없습니다. 미래의 달에서 빌리는 것.

갱신에 실패 표시하고 있지 페이지를 얻으려면, 나는 값이 모델에 존재하지 않았기 때문에 attr_accessor이 (그렇지 않으면 내가 업데이트에 대한 장애를 가지고 있음을 추가했습니다.

발췌을 내 코드에서이다 오류는 없지만 attr_accessor에 반영된 변수에 대한 업데이트가 없습니다. 자식 객체의 일반적인 요소에 대한 변경을 테스트 해 보았습니다. 업데이트 될 예정이지만 attr_accessor " 세터 ".

제안?

감사합니다. , 카밀 .. 접근의

class Projectreview < ActiveRecord::Base 
    has_many :reviewcostelements 

    accepts_nested_attributes_for :reviewcostelements 

end 

class ProjectreviewsController < ApplicationController 
    def update 
    @projectreview = Projectreview.find(params[:id]) 


    respond_to do |format| 
     if @projectreview.update_attributes(params[:projectreview]) 
     format.html { redirect_to(@projectreview) } 
     end 
    end 
    end 


end 

class Reviewcostelement < ActiveRecord::Base 
    belongs_to :projectreview 


    attr_accessor :monthahead_hours1 
    def monthahead_hours1(newvalue) #this is the setter 
    #why do I never see this log message ?? 
    logger.info('SETTER 1') 
    set_monthahead_hours(1, newvalue) 
    end 

    def monthahead_hours1 #this is the getter 
     get_monthahead_hours(1) 
    end 

    def update_attributes(attributes) 
    #never gets called!!! 
    logger.info('update_attributes values rce') 
    super(attributes) 
    end 

    def get_monthahead_hours(p_monthsahead) 
    #this works and returns the next month's scheduled_hours_this_month value 
    rce = Reviewcostelement.first(:joins => :projectreview, 
             :conditions => ["projectreviews.project_id = ? 
             and reviewcostelements.projecttaskcode_id =? 
             and projectreviews.month_start_at = ?", projectreview.project_id , 
        projecttaskcode_id , 
        projectreview.month_start_at.months_since(p_monthsahead)]) 
    if rce 
     return rce.scheduled_hours_this_month 
    else 
     return 0 
    end 
    end 

    def set_monthahead_hours(p_monthsahead, newvalue) 
    #this never seems to get called 
    logger.info("set the month ahead hours") 

    rce = Reviewcostelement.first(:joins => :projectreview, 
             :conditions => ["projectreviews.project_id = ? 
             and reviewcostelements.projecttaskcode_id =? 
             and projectreviews.month_start_at = ?", 
        projectreview.project_id , 
        projecttaskcode_id , 
        projectreview.month_start_at.months_since(p_monthsahead)]) 
    if rce 
     rce.scheduled_hours_this_month = newvalue 
     rce.save 
    end 
    end 
end 

답변

2

세터는 다음과 같다 :

def monthahead_hours1=(newvalue) 
    ... 
end 

참고 = 같은 기호.

관련 문제