I am new to ruby and ruby on rails and I am trying to build a blog. This blog has articles, which are extending ActiveRecord::Base.
They have also some relations to tags or comments.
Now, I want to have the possibility to load articles from other sources than the DB. For example from my website, which is not the same app and provides a REST API to load articles.
How could I do this?
With an ActiveArticle which extends ActiveRecord::Base and an ExternalArticle which does not extends and an ArticleModule to implement the same features?
That seems to be not the right way. Thanks
A good chunk of what makes up a Rails model lives not in ActiveRecord
, but in ActiveModel
.
class ExternalArticle
include ActiveModel::Model
end
will give you functionality like validations, callbacks, compatibility with URL helpers and so on.
See Railscast #219 for a short overview.
However, ActiveModel does not give you associations to other models. If you need those, the activerecord-tableless gem might help.
Edit:
I see now that your non-persisted model objects come from a REST API. Take a look at ActiveResource as well; it used to be part of Rails, but has been extracted into a separate gem as of Rails 4. It is basically ActiveRecord for remote objects.