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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
If you are using rails 3, see this http://railscasts.com/episodes/219-active-model On Wed, Oct 27, 2010 at 4:06 PM, Satsou Sa <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
yong gu wrote in post #957495:> If you are using rails 3, see this > http://railscasts.com/episodes/219-active-modelThanks! But there is a problem with ActiveModel: I can not make associations such as belongs_to or has_many... -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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? On Wed, Oct 27, 2010 at 10:43 PM, Satsou Sa <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> yong gu wrote in post #957495: > > If you are using rails 3, see this > > http://railscasts.com/episodes/219-active-model > > Thanks! But there is a problem with ActiveModel: I can not make > associations such as belongs_to or has_many... > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Satsou Sa wrote in post #957508:> yong gu wrote in post #957495: >> If you are using rails 3, see this >> http://railscasts.com/episodes/219-active-model > > 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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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). -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.