# has\_many relationship able to be placed on one to others?

**URL:** <https://discuss.rubyonrails.org/t/has-many-relationship-able-to-be-placed-on-one-to-others/35645>\
**Category:** rubyonrails-talk\
**Created:** [October 23, 2008, 8:14am UTC](https://discuss.rubyonrails.org/t/has-many-relationship-able-to-be-placed-on-one-to-others/35645 "2008-10-23T08:14:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [October 23, 2008, 8:14am UTC](https://discuss.rubyonrails.org/t/has-many-relationship-able-to-be-placed-on-one-to-others/35645/1 "2008-10-23T08:14:57Z")

</div>

It's time for me to go to bed... but perhaps I'll wake up to a bit of good advice.

I have 3 tables: &nbsp;&nbsp;registrations &nbsp;&nbsp;cars &nbsp;&nbsp;motorcycles

I am trying to define the registration object in this way: &nbsp;&nbsp;has\_many :cars &nbsp;&nbsp;has\_many :motorcycles

Then for the cars and motorcycles I'm saying &nbsp;&nbsp;belongs\_to :registration

Something is quite wrong here. How should I be relating these? I read this tutorial: [http://railsguides.phusion.nl/activerecord/association\_basics.html](http://railsguides.phusion.nl/activerecord/association_basics.html)

I keep referencing back to this, but I cannot find a relationship that fits.

I'm sure I'm not understanding something properly about how relationships should work. I just can't wrap my head around the "right way" to do things when setting up relationships. Frustrating. Good night all.

---

<div class="post-metadata">

**Author:** ![Thorsten\_Muller2](https://avatars.discourse-cdn.com/v4/letter/t/8edcca/32.png) [@Thorsten\_Muller2](https://discuss.rubyonrails.org/u/Thorsten_Muller2)\
**Post date:** [October 23, 2008, 9:35am UTC](https://discuss.rubyonrails.org/t/has-many-relationship-able-to-be-placed-on-one-to-others/35645/2 "2008-10-23T09:35:13Z")

</div>

hmmm, you don't give enough information. What's the registration about? Is this for some kind of car rental service?

The way you describe it, a car could only belong to a single registration. Another thing missing is the customer who makes the registration.

If it's like a car rental, then the registration should be polymorphic and either belong to a car or motorcycle.

registration: belongs\_to :registrateable, :polymorphic =\> true belongs\_to :customer # it needs a registrateable\_id column in which you store the car or motorcycle id # and a registrateable\_type column in which Rails stores the class

Can you say registrateable? I'm not native english, maybe there is a better word? registerable?

Cars and motorcycles each get: has\_many :registrations, :as =\> :registrateable

This way you could get as many links between customers and cars as you want.

---

<div class="post-metadata">

**Author:** ![gnunix](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@gnunix](https://discuss.rubyonrails.org/u/gnunix)\
**Post date:** [October 23, 2008, 8:34pm UTC](https://discuss.rubyonrails.org/t/has-many-relationship-able-to-be-placed-on-one-to-others/35645/3 "2008-10-23T20:34:33Z")

</div>

> hmmm, you don't give enough information. What's the registration about? Is this for some kind of car rental service?

I sure didn't. 3:30am was late for me. 🙂

It is a warranty registration where many different products need to be registered. Each product shares some of the same characteristics, date\_registered, dealer\_purchased\_from, etc... those are kept in the registrations table. But each has it's own unique qualities that we need to track. Perhaps a serial\_number and model\_number and power\_pack\_sn for one product. The other might have a serial\_number and a blade\_sn, for instance.

> The way you describe it, a car could only belong to a single registration. Another thing missing is the customer who makes the registration.
> 
> If it's like a car rental, then the registration should be polymorphic and either belong to a car or motorcycle.
> 
> registration: belongs\_to :registrateable, :polymorphic =\> true belongs\_to :customer # it needs a registrateable\_id column in which you store the car or motorcycle id # and a registrateable\_type column in which Rails stores the class
> 
> Can you say registrateable? I'm not native english, maybe there is a better word? registerable?

Registerable is in fact a word. 🙂

> Cars and motorcycles each get: has\_many :registrations, :as =\> :registrateable
> 
> This way you could get as many links between customers and cars as you want.

I don't think this relationship would work. Because we are dealing with serialized inventory. So product A is really an assembly that consists of several different serialized parts and can only be registered once to 1 customer. Product B is also an assembly that consists of several different serialized parts that can only be registered to a single customer. The customer can have many different products all with unique registrations. Does that help?

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [October 24, 2008, 6:30am UTC](https://discuss.rubyonrails.org/t/has-many-relationship-able-to-be-placed-on-one-to-others/35645/4 "2008-10-24T06:30:20Z")

</div>

Michael Kahle wrote:

> I don't think this relationship would work. Because we are dealing with serialized inventory. So product A is really an assembly that consists of several different serialized parts and can only be registered once to 1 customer. Product B is also an assembly that consists of several different serialized parts that can only be registered to a single customer. The customer can have many different products all with unique registrations. Does that help?

Shows what I know... nothing. I have been struggling with this for the past few hours (slow learner?), but upon further review I believe this is exactly what I need.

I have built the relationships, but now I am trying to find examples on how I would setup the controller for each product to handle the registration.

I'll post again when I know more, but thanks for this. I believe it is precisely the direction I should be going.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [October 24, 2008, 9:03am UTC](https://discuss.rubyonrails.org/t/has-many-relationship-able-to-be-placed-on-one-to-others/35645/5 "2008-10-24T09:03:09Z")

</div>

Bed time again. Strange how hyper-focused I can be at night.

I've been working off of a post to get me going on understanding how to implement in Rails these polymorphic associations. [http://railsforum.com/viewtopic.php?id=5882](http://railsforum.com/viewtopic.php?id=5882)

I also found this:

> **[Insight: Medical Device Software Articles and Resources](https://orthogonal.io/insights/)**
>
> Orthogonal provides timely news, literature reviews, articles, white papers, and commentary for a medical device manufacturing audience.

Looks promising, but at this level of abstraction I have trouble understanding.

I'm afraid I'm trying to run before I can walk.
