Wednesday, September 21, 2011

Re: [Rails] Model that belongs to Person or Company

On Sep 21, 2011, at 9:52 PM, Rodrigo Ruiz wrote:

> Hi, I gotta make a model (telefone) that belongs to a person or to a
> company, how do I do that?
>
> Thank you,
> Rodrigo

You probably want a polymophic association:

class Telefone < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
end

class Person < ActiveRecord::Base
has_many :telefones, :as => :owner
end

class Company < ActiveRecord::Base
has_many :telefones, :as => :owner
end

See the information at
http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

-Rob

Rob Biedenharn
Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab@GaslightSoftware.com http://GaslightSoftware.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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate