Marli Ba
2011-Oct-20 17:18 UTC
Access other model attributes directly with has_one, belongs_to ?
So, I have 2 models A and B that share 5 common attributes and all other attributes are different. So I wanted to extract these 5 out into one common table and use has_one, belongs_to to knit it back together. So now there are 3 tables with 1 having the shared properties, we''ll call this table C. Table A id dollar_amount Table B id quantity_on_hand Table C id version My question is, I want to access attributes from table C like they are in table A, such as: @a = a.new a.version and NOT a.c.version Is my only option to use method_missing? Anyone got 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.
Tim Shaffer
2011-Oct-20 19:24 UTC
Re: Access other model attributes directly with has_one, belongs_to ?
I think what you''re looking for is "delegate" which is a rails extension to Module: http://api.rubyonrails.org/classes/Module.html#method-i-delegate class ModelC < ActiveRecord::Base has_many :model_a end class ModelA < ActiveRecord::Base belongs_to :model_c delegate :version, :to => :model_c end Then you can call version on a ModelA instance like you want to. -- 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/-/579ERCsRs0QJ. 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.
Marli Ba
2011-Oct-20 19:41 UTC
Re: Access other model attributes directly with has_one, belongs_to ?
Thanks, I think that is what I''m looking for! -- 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.