# Setting object most prefered from has\_many

**URL:** <https://www.ruby-forum.com/t/setting-object-most-prefered-from-has-many/129875>\
**Category:** Rails\
**Created:** [February 22, 2008, 10:17am UTC](https://www.ruby-forum.com/t/setting-object-most-prefered-from-has-many/129875 "2008-02-22T10:17:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [February 22, 2008, 10:17am UTC](https://www.ruby-forum.com/t/setting-object-most-prefered-from-has-many/129875/1 "2008-02-22T10:17:19Z")

</div>

I’d create a table (as persons) related to another (as animals), to  
selecting the animals prefered for each person but indicating too  
wich is the most prefered. My idea would be use ‘has\_many’ to relating  
some animals and ‘has\_one’ for the most prefered (that would not be in  
the another relation).

Do you know another solution better that this? Thanks in advance!

* * *

create\_table :persons do |t|  
…  
has\_many :animals # Animales prefered  
has\_one :animals # Animal most prefered  
end

## create\_table :animals do |t| t.string :name … belongs\_to :persons end

---

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [February 22, 2008, 11:10am UTC](https://www.ruby-forum.com/t/setting-object-most-prefered-from-has-many/129875/2 "2008-02-22T11:10:29Z")

</div>

two of them:

1. make it an acts\_as\_list and allow to sort animals by preference (but  
lots of more work)

2. just have a flag prefered in animals  
then in persons

class Person \< ActiveRecord::Base  
has\_many :animals do  
def prefered  
find(:first, :conditions =\> {:prefered =\> true})  
end  
end

---

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [February 22, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/setting-object-most-prefered-from-has-many/129875/3 "2008-02-22T12:02:57Z")

</div>

On 22 feb, 10:10, Thorsten M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 1. just have a flag prefered in animals  
> then in persons
> 
> class Person \< ActiveRecord::Base  
> has\_many :animals do  
> def prefered  
> find(:first, :conditions =\> {:prefered =\> true})  
> end  
> end  
> But with this option, does each person could have a different prefered  
> animal?

---

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [February 22, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/setting-object-most-prefered-from-has-many/129875/4 "2008-02-22T12:19:00Z")

</div>

Kless wrote:

> But with this option, does each person could have a different prefered  
> animal?

Not easily. There is no way (AFAIK) to put a relationship between two  
relations in Rails that enables the related has\_many model to remain  
independent of the primary model. I’m happy to be proven wrong…

One solution is to override methods in your Person model for setting the  
animals and preferred animal. These methods can do the validation, so  
setting the preferred animal checks it is contained in the animals list  
and removing an animal from the animals list can check if it is the  
preferred animal (and either fail or remove the preferred animal  
depending on how you want it to function).

Your unit tests will be important here…

---

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [February 22, 2008, 12:14pm UTC](https://www.ruby-forum.com/t/setting-object-most-prefered-from-has-many/129875/5 "2008-02-22T12:14:27Z")

</div>

Kless wrote:

> On 22 feb, 10:10, Thorsten M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > 1. just have a flag prefered in animals  
> > then in persons
> > 
> > class Person \< ActiveRecord::Base  
> > has\_many :animals do  
> > def prefered  
> > find(:first, :conditions =\> {:prefered =\> true})  
> > end  
> > end  
> > But with this option, does each person could have a different prefered  
> > animal?

the way you described your setup:  
animal belongs\_to person

would mean, that a animal record can belong to only one person  
so i assumed, that you talk about ‘named’ animals  
so a person has a cat named “foo”  
another person a cat named “bar”

if you want to have a set of animals (horse, cat, dog)  
of which each person can chose some and set one as prefered  
you would need a habtm relationship  
each person can have many animals  
each animal can belong to many persons

in your case, where you need additional information (prefered)  
you should create an intermediate model

PersonsAnimals  
belongs\_to :person  
has\_many :animals

and keep the prefered flag and it’s finder method in this model  
and of course you can make use og :through

Person  
has\_many :persons\_animals  
has\_many :animals :through =\> :persons\_animals

---

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [February 22, 2008, 12:22pm UTC](https://www.ruby-forum.com/t/setting-object-most-prefered-from-has-many/129875/6 "2008-02-22T12:22:49Z")

</div>

Mark B. wrote:

> Not easily. There is no way (AFAIK) to put a relationship between two  
> relations in Rails that enables the related has\_many model to remain  
> independent of the primary model. I’m happy to be proven wrong…

OK, yes, I missed the fact this is really (by the sound of it) a  
has\_and\_belongs\_to\_many relationship and so Thorsten is obviously right.  
An intermediate relationship is the way to go. Preference is then  
defined there.

---

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [February 22, 2008, 1:14pm UTC](https://www.ruby-forum.com/t/setting-object-most-prefered-from-has-many/129875/7 "2008-02-22T13:14:04Z")

</div>

Thank you very much for the help.
