# Question on Designing Model Associtations

**URL:** <https://www.ruby-forum.com/t/question-on-designing-model-associtations/143125>\
**Category:** Rails\
**Created:** [July 15, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/question-on-designing-model-associtations/143125 "2008-07-15T14:28:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [July 15, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/question-on-designing-model-associtations/143125/1 "2008-07-15T14:28:47Z")

</div>

Hi all,  
I try to create a little game, my first application in Ruby and Rails.  
And I’ve problems understanding polymorphic associations between  
models. I have a model called “Travel”. Two models, “Character” and  
“Truck” can participate in a travel. How should I setup the models ?

I tried it this way:  
class Travel \< ActiveRecord::Base  
has\_many :travellers  
end  
class Traveller \< ActiveRecord::Base  
belongs\_to :travel  
belongs\_to :travelable, :polymorphic =\> true  
end  
class Character \< ActiveRecord::Base  
has\_one :traveller, :as =\> :travelable  
end  
class Truck \< ActiveRecord::Base  
has\_one :traveller, :as =\> :travelable  
end

Not only that it’s not working, it’s also not a very clean approach. I  
think that’s easy to explain for a more experienced developer than me,  
so please give me some hints.

Thank you very much in advance and greetings from Austria  
Christoph

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [July 15, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/question-on-designing-model-associtations/143125/2 "2008-07-15T14:56:56Z")

</div>

setup seems ok, assuming that Traveller is the poly model  
and has the columns travelable\_id and travelable\_type

Otherwise it would be helpful, to know what is not working  
and what exactly you want to do here. (And why you think  
it’s not a clean approach)  
With games it’s especially difficult to guess your intentions.

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [July 15, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/question-on-designing-model-associtations/143125/3 "2008-07-15T19:15:29Z")

</div>

> Otherwise it would be helpful, to know what is not working  
> and what exactly you want to do here.  
> Ok, Traveller has these columns:  
> create\_table “travellers”, :force =\> true do |t|  
> t.integer “travel\_id”  
> t.integer “travelable\_id”  
> t.integer “travelable\_type”  
> end  
> When I do this in script/console  
> Travel.last.travellers \<\< Character.last  
> I get the following error:  
> ActiveRecord::AssociationTypeMismatch: Traveller(#16747500) expected,  
> got Character(#16661940)

(And why you think it’s not a clean approach)  
I think it’s not very clean, because I need an extra Traveller model.  
I would like to avoid this if it’s possible. The situation is the  
following: Both Characters and Trucks can travel. One Travel Record  
holds the from/to location and through a 1:n relationship either the  
participating characters/trucks. I hope my English is understandable.

Greetings  
Christoph
