简体   繁体   中英

Tableless model in Rails

I want to create tableless model which doesn't need datebase. At example:

class Post < ActiveRecord::Base
  attr_accessible :body, :title, :language_id
  belong_to :language
end
class Language
  has_many :post
  ...
end

Will be 2 or 3 language. I don't want to load DB, is it possible to create languges in model by hand?

It might help to read this article: http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/ .

In general, your models need not inherit from ActiveRecord, because you can include ActiveModel instead.

On the other hand, you can keep it simple like so:

class Langauge
  attr_accessor :posts
  def initialize
    @posts = []
  end

  def add_post(post)
    @posts << post
  end
end

lang = Language.new
lang.add_post(Post.new) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM