Ruby on Rails Monday, November 1, 2010

Now there is also the fact that user has to know what they own.
If I go with the

belongs_to :ownable, polymorphic => true.

From user then user.ownable will either return company,
real_state_company or natural user.
I would have to do user.ownable_type to see which one and use a string
comparisson.

If I go the other route:

has_one :real_state_company
has_one :construction_company
has_one :natural_person

I can say,

if(user.construction_company != nil)
{
company = user.company
redirect_to company
}
else if (user.real_state_company)
{
....
}
etc.

Which one is cleaner, better design ?

Thanks
-dan

On Nov 1, 1:46 pm, Tim Shaffer <timshaf...@me.com> wrote:
> On Nov 1, 2:18 pm, dpal <dan...@gmail.com> wrote:
>
> > Unfortunatelly has_one polymorphic is not supported, therefore we need
> > to think in reverse.
>
> That's because if the "belongs_to" is on the company models, it's no
> longer a polymorphic relationship. At that point it's just a regular
> one-to-one relationship, like the following example. Each of the
> companies has a foreign key to Person.
>
> class RealStateCompany
>   belongs_to :user
> end
>
> class ConstructionCompany
>   belongs_to :user
> end
>
> class NaturalPerson
>   belongs_to :user
> end
>
> class User
>   has_one :real_state_company
>   has_one :construction_company
>   has_one :natural_person
> end
>
> This works. Reversing the relationship also works. Just depends on how
> you want to design it and where you want the foreign keys.

--
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.

No comments:

Post a Comment