# Help with Favourite model

**URL:** <https://www.ruby-forum.com/t/help-with-favourite-model/178518>\
**Category:** Rails\
**Created:** [November 5, 2009, 8:29pm UTC](https://www.ruby-forum.com/t/help-with-favourite-model/178518 "2009-11-05T20:29:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2009, 8:29pm UTC](https://www.ruby-forum.com/t/help-with-favourite-model/178518/1 "2009-11-05T20:29:54Z")

</div>

I have a User model, a Favourite model, a Film model and a Song model.  
I would like o achieve user favourite films and user favourte songs with  
databse associations, using just a Favourite model. Which will be the  
best way? How should define those models?

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2009, 9:00pm UTC](https://www.ruby-forum.com/t/help-with-favourite-model/178518/2 "2009-11-05T21:00:11Z")

</div>

John S. wrote:

> I have a User model, a Favourite model, a Film model and a Song model.  
> I would like o achieve user favourite films and user favourte songs with  
> databse associations, using just a Favourite model. Which will be the  
> best way? How should define those models?

I assume the best way will be something like that:  
create\_table :favorites do |t|  
t.integer :user\_id  
t.references :favorable, :polymorphic =\> true  
t.timestamps  
end

User.rb  
has\_many :favorites  
has\_many :favorables, :through =\> :favorites

Film.rb  
has\_many :favorites, :as =\> :favorable  
has\_many :favoriting\_users, :through =\> :favorites, :source =\> :user

Song.rb  
has\_many :favorites, :as =\> :favorable  
has\_many :favoriting\_users, :through =\> :favorites, :source =\> :user
