2014-10-13 2 views
1

모달 클래스의 인스턴스에서 작동하는 메서드를 모달로 정의해야할지 또는 모달 인스턴스를 매개 변수로 전달할지 여부를 결정하는 방법을 설명 할 수 있습니까? 내가 VisitorMarketing라는 모달이모델에 메서드를 작성하거나 모델을 매개 변수로 전달하십시오.

: 여기

은 예입니다. 방법을 정의 할 필요가 start_marketingstop_marketing.

나는이 같은 모달 방문자에서 그들을 만들 수 있습니다

class Visitor < ActiveRecord::Base 
    has_many :marketing 

    def start_marketing(options) 
     # massage the passed in attribute here 
     # then make api calls 
     # then do some business logic here 
     self.marketing.create options 
    end 

    def stop_marketing(options) 
     # massage the passed in attribute here 
     # then make api calls 
     # then do some business logic here 
     marketing = self.marketing.find options 
     marketing.update_attributes {:mark_stopped => true} 
    end 
    end 

을 또는이 같은 마케팅 모달에서 그들을 만들 수 있습니다

class Marketing < ActiveRecord::Base 
    belongs_to :visitor 

    def self.start(visitor, options) 
     # massage the passed in attribute here 
     # then make api calls 
     # then do some business logic here 
     Marketing.create {:visitor_id => visitor.id}.merge(options) 

    end 

    def self.stop(visitor,options) 
     # massage the passed in attribute here 
     # then make api calls 
     # then do some business logic here 
     marketing.find_by_visitor_id_and_id options[:id], options[:visitor_id] 
     marketing.update_attributes {:mark_stopped => true} 
    end 
    end 

이들은 단지 예이다 내가 혼란스러워하는 지점을 강조하십시오. 실제로 이러한 메서드는 레코드를 업데이트하는 것 외에도 인스턴스에서 더 많은 작업을 수행합니다.

답변

0

질문을 이해합니다. 첫 번째 버전은 레일 방식입니다. 한 가지 경우에는 내장 된 연관 메소드를 사용하고 두 번째 예제에서와 같이 주위에 ID를 전달하지 않아야합니다.

내가 가진 정확한 문제에 대한 세부 정보가 없으면 확실한 대답을 드릴 수 없습니다.

+0

첫 번째 접근 방식을 사용할 때'방문자 '모달에 너무 많은 방법이 있다고 생각합니까? 첫 번째 접근 방식에서는 '마케팅'모달에 대한 기록 작성이 이루어 지지만 메소드는 '방문자'모달에 추가됩니다. 이것은'visitor' 모달 팻을 매우 빠르게 만들 것입니다. 귀하의 의견? – user566245

관련 문제