Suppose that in legacy schema,that you are working on getting ActiveRecord to work with, you aren''t keeping all the information about user, with the user''s main information in the users table, but in a separate table. For example: <<table users>> user_id firstname lastname <<table auth_info>> user_id <<PK>><<FK>> username, password In my domain I wish to have User model with username,password properties without create class model related to auth_info table and relative association. Is there a way to do this with ActiveRecord ? -- 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.
Nicolas Sebrecht
2011-Sep-19 14:24 UTC
Re: splitted class into separate table activerecord
The 13/09/11, Vogon Primo wrote:> Suppose that in legacy schema,that you are working on getting > ActiveRecord to work with, you aren''t keeping all the information about > user, with the user''s main information in the users table, but in a > separate table. > > For example: > > <<table users>> > user_id > firstname > lastname > > <<table auth_info>> > user_id <<PK>><<FK>> > username, > password > > In my domain I wish to have User model with username,password properties > without create class model related to auth_info table and relative > association. Is there a way to do this with ActiveRecord ?I would redefine the method_missing feature (Ruby) in the model User. Something like class User def method_missing(sym, *args, &block) if (user_auth_info and user_auth_info.respond_to? name) then return auth_info.send(sym, *args, &block) else super end end end This is NOT TESTED. -- Nicolas Sebrecht -- 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.
You can also use the Rails delegate feature. I know you said you didn''t want to create the the AuthInfo model but I''m not sure there''s any harm to it? class User has_one :auth_info delegate :username, :username=, :password, :password=, :to => :auth_info end class AuthInfo belongs_to :user end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/M8XV1aATM1EJ. 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.