2014-06-10 2 views
0

나는 서비스에서 Users을지도하는 ActiveResource 객체를 사용하고 싶지만 난 내가 이런 걸 보이는 코드가 이러한 UsersActiveResource 객체에서 has_many 메서드를 사용하여 로컬 ActiveRecord 객체에 연결 하시겠습니까?

와 연결하려는 로컬 데이터가 있습니다

user.rb를 :

class User < ActiveResource::Base 
    self.site = "http://my/api" 

    has_many :notifications, as: :notifiable, dependent: :destroy 
    attr_accessible :notifications_attributes 
    accepts_nested_attributes_for :notifications, allow_destroy: true 
end 

notification.rb :

class Notification < ActiveRecord::Base 
    belongs_to :notifiable, polymorphic: true 
    belongs_to :user 
    belongs_to :recipient, class_name: 'User' 
end 

ActiveResourcehas_many을 지원하지 않는 경우이 문제를 어떻게 해결해야합니까?

답변

3

ActiveResource는 연결을 지원하지만 외부 리소스 여야합니다. 따라서 귀하의 경우에는 적용되지 않습니다.

귀하의 요구 사항이 합법적이며 수동으로 제안 된 방법이라고 생각합니다. 예 :

class User < ActiveResource::Base 
    self.site = "http://my/api" 

    def notifications 
    Notification.where(user_id: self.id) 
    end 
end 

class Notification < ActiveRecord::Base 
    belongs_to :notifiable, polymorphic: true 

    def user 
    User.find(self.user_id) 
    end 

    def recipient 
    User.find(self.recipient_id) 
    end 
end 
+0

Ahhh ok. 나는 이것을 시험해야 할 것 같아. 나는 지금 당장 +1하고 나중에 받아 들여야한다. 이것이 단단한 해결책이된다면. – DJTripleThreat