2011-08-10 8 views
1

이 문제는 Rails 3.0.7 및 3.0.9, WEBrick 및 Apache에서 발생합니다.read_inheritable_attribute가 예기치 않게 수정되었습니다.

module Reportable 
    module ClassMethods 
    def add_report(report_name) 
     instance_eval do 
     write_inheritable_hash(:reportable_report_names, 
      {report_name => {:dates => true, :details => 'something'}) 
     end 
     end 
    end 
    end 

    def self.included(base) 
    base.extend(ClassMethods) 
    end 
end 

Reportableconfig/initializers에로드되고, 클래스가 사용 : 생산 모드

class User < ActiveRecord::Base 
    include Reportable 
    add_report :report1 
    add_report :report2 
end 

를 처음

는 I는 inheritable_attribute 쓰기 방법을 갖는 모듈 Reportable을 서버가 시작된 후 페이지를로드하면 속성이 올바르게로드됩니다.

User.read_inheritable_attribute(:reportable_report_names) 
# => {:report1 => {:dates => true, :details => 'something'}, 
     :report2 => {:dates => true, :details => 'something'}} 

그러나 두 번째 페이지로드에:

개발 예상, 생산 모드에서 콘솔대로 작동
User.read_inheritable_attribute(:reportable_report_names) 
# => {:report1 => {:dates => true}, 
     :report2 => {:dates => true}} 

. 문제는 프로덕션 모드의 웹 서버에있는 POST 요청에만 나타납니다. 뭐라 구요?

+0

가능한 해결 방법은 클래스를 언로드 가능하게 만드는 것입니다. 여기 당신이 설명을 찾을 수 있습니다 : http : // stackoverflow.com/questions/6853471/ruby-on-rails-unloadable – socjopata

답변

1

을의 원인은 read_inheritable_attribute이 값이 아닌 참조를 반환했기 때문입니다.

오류를 던진 동작이 문제의 진정한 원인 인 다른 동작으로 리디렉션되었습니다. 이 모듈은 다른 방법이 있었다 :

def available_reports 
    read_inheritable_attribute(:reportable_report_names) 
end 

을 그리고 다른 행동이 이런 짓을 : 참조를 반환

# @model_reports = a hash of 'model_class_name'.to_sym => model.available_reports.dup 
@model_reports.each do |model, model_reports| 
    model_reports.each do |name, properties| 
    properties = properties.keep_if... # remove per the view's requirements 
    end 
end 

available_reports 때문에, dup는 아무것도하지 않았다 및 keep_if는 클래스 변수에서 내 소중한 값을 삭제.

수정했다 : 제대로 답변에 충분한 정보가 아니 었더라도 체크 아웃이 질문에 시간이 걸렸습니다 사람들에게

properties = properties.dup.keep_if... 

감사합니다.

+0

의견에 감사드립니다. – apneadiving

1

방금 ​​시도했지만 문제없이 모든 환경에서 작동합니다. 다른 세부 사항?


그렇지 않으면 : 당신이 모델 자체에 최선을 다하고 있기 때문에

  • 은 왜 instance_eval를 사용합니까?

  • 당신이 이어질 것 인수

전달하는 플랫를 사용한다

  • :

    module ClassMethods 
        def add_reports(*report_names) 
        report_names.each do |report_name| 
         write_inheritable_hash(:loggable_report_names, 
         {report_name => {:dates => true, :details => 'something'}) 
         end 
        end 
        end 
    end 
    

    그리고 모델 : 두통의 많은 후

    include Reportable 
    add_reports :report1, :report2 
    
  • +0

    간단 해졌습니다 ...'Reportable'은 많은 클래스들에 포함되어 있습니다. 각 클래스는 다른 리포트 세트를 가지고 있습니다. 각 보고서는 보고서 메서드를 만드는 데 사용되는 이름과 보고서 메서드 이름을 키로 사용하여': loggable_report_names'에 저장된 옵션의 해시를 사용하여 생성됩니다. 또한 모듈에 보고서 이름과 옵션을 쉽게 검색 할 수있는 여러 가지 방법이 있습니다. – jimworm

    +0

    대답이 아니라 ... 어쨌든 도움을주는 upvote. 감사! read_inheritable_attribute가 참조가 아닌 값을 반환 할 것으로 기대하고있었습니다. – jimworm

    +0

    '포함 된'블록에 개체를 만들 수 있습니까? – apneadiving

    관련 문제