Virtual model

Hello,

I would like to create a simple ActiveRecord model, but working without table... Is that possible? I think we can this a virtual model.

And I would like to be able to use relations (like has_many :users, etc) with this model (named App for instance), such as:

  > App.first.users

Thanks for any ideas.

If you are using rails 3, see this http://railscasts.com/episodes/219-active-model

yong gu wrote in post #957495:

If you are using rails 3, see this #219 Active Model - RailsCasts

Thanks! But there is a problem with ActiveModel: I can not make associations such as belongs_to or has_many...

I think if you use belongs_to or has_many, you must have to persist the model. Or how can you retrieve the relationships

later?

Satsou Sa wrote in post #957508:

yong gu wrote in post #957495:

If you are using rails 3, see this #219 Active Model - RailsCasts

Thanks! But there is a problem with ActiveModel: I can not make associations such as belongs_to or has_many...

Why do you need them on a virtual model? Just write your own method.

belongs_to and has_many aren't really useful if there's no DB table, are they? What are you trying to achieve?

Best,

Yes Marnen, I agree; it's not really useful. But I prefer to do this rather then having a table with only 1 row. That's why I would like to get a singleton model with a static data (and always the same id for associations with other models).

Please quote when replying.

Satsou Sa wrote in post #957750:

Yes Marnen, I agree; it's not really useful. But I prefer to do this rather then having a table with only 1 row.

And you can.

That's why I would like to get a singleton model with a static data (and always the same id for associations with other models).

Why do you need an ID? Again, please explain in more detail what you are trying to do. has_many and belongs_to are only useful when dealing with SQL, as I see it. Otherwise, you don't need them; a non-SQL approach could be as simple as

class Book < ActiveModel   attr_accessor :pages

  def initialize     @pages =   end end book = Book.new

...and then book.pages, book.pages.count, and book.pages << Page.new all work as expected. That's just the way OO development works.

has_many and belongs_to are a hack to make SQL databases feel more OO. You don't need them if there's no SQL.

Best,