How to create an ActiveRecord relation to an ActiveResource object?

Say I'm writing a library application for a publishing company that already has a People application.

So in my Library application I have

class Person < ActiveResource::Base
  self.site = "http://api.people.mypublisher.com/"
end

and now I want to save Articlefor each Person:

class Article < ActiveRecord::Base
  belongs_to :person, :as => :author
end

I assume that I will have the following table in my database:

Articles
id (PK) | title (string) | body (text) | author_id (integer)

author_id- This is not a foreign key, since I do not have a People table. This leaves a few questions:

  • How can I tell my object Person ActiveResourcethat it is has_many Articles?

  • Will it work Articles.find(:first).author? Will it work belongs_toprovided that there is no ActiveRecordand no support table?

+5
source share
3

, , ActiveResource , ActiveRecord.

№1. № 2, ActiveRecord , "" ActiveResource. Aritcle.find(: first).author .

+2

, # 1, , , :

class Person < ActiveResource::Base
  self.site = "http://api.people.mypublisher.com/"

  def articles
    Article.find(:all, :conditions => { :person_id => self.id })
  end

  def add_article(article)
    article.person_id = self.id
  end
end

, has_many.

+5

, , .

class Person < ActiveResource::Base
  self.site = ..
. 
  def articles
    Article.for_person(self.id)
  end
end

class Article < ActiveRecord::Base
  named_scope :for_person, lambda { |pid| { :conditions => { :person_id => pid }}}
end
0

All Articles