# Activerecord has\_many vs. has\_one scope

**URL:** <https://www.ruby-forum.com/t/activerecord-has-many-vs-has-one-scope/178004>\
**Category:** Rails\
**Created:** [October 27, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/activerecord-has-many-vs-has-one-scope/178004 "2009-10-27T21:49:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [October 27, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/activerecord-has-many-vs-has-one-scope/178004/1 "2009-10-27T21:49:20Z")

</div>

Hi All,

in the api for has\_many, under the :conditions option we see :

```
Specify the conditions that the associated objects must meet in 

```

order to  
be included as a WHERE SQL fragment, such as price \> 5 AND name LIKE  
â€˜B%â€™.  
Record creations from the association are scoped if a hash is used.  
has\_many[http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001885&name=has\_many](http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001885&name=has_many):posts,  
:conditions =\> {:published =\> true}will create published posts with  
@blog.posts.create or @blog.posts.build.

however, in the api for has\_one under the :conditions option we see :

```
Specify the conditions that the associated object must meet in order 

```

to  
be included as a WHERE SQL fragment, such as rank = 5.

So, we can’t automatically scope associated has\_one records by  
conditions  
like we can has\_many. seems to me that the behavior should be the same  
in  
both situations.

I have a model with relationships like this

class ThingContainer \< AR::Base  
has\_one :blue\_thing, :class\_name =\> “Thing”, :conditions =\> {:color =\>  
“blue”}  
has\_one :red\_thing, :class\_name =\> “Thing”, :conditions =\> {:color =\>  
“red”}  
end

class Thing \< AR::Base  
belongs\_to :thing\_container  
end

but when I call ThingContainer.build\_blue\_thing, it does not populate  
the  
color attribute. is this by design? or something that was simply left  
out?

-C

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [October 27, 2009, 10:50pm UTC](https://www.ruby-forum.com/t/activerecord-has-many-vs-has-one-scope/178004/2 "2009-10-27T22:50:13Z")

</div>

You appear to be trying to fit has\_one into an implementation it  
wasn’t designed for. The has\_one is supposed to be exactly that:  
ThingContainer has\_one Thing, but you actually have ThingContainer  
has\_many :things # well, really one of each of different kinds.

There are likely several ways to handle this situation, but my  
instinct is to use named scopes for the color finds and validations to  
enforce the one-of-each. Another way would be to use STI to set up  
color models that inherit from Thing, with the has\_ones from  
ThingContainer pointing to each.

-eric

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [October 28, 2009, 2:28am UTC](https://www.ruby-forum.com/t/activerecord-has-many-vs-has-one-scope/178004/3 "2009-10-28T02:28:00Z")

</div>

This was just fixed on September 28:

[https://rails.lighthouseapp.com/projects/8994/tickets/3088](https://rails.lighthouseapp.com/projects/8994/tickets/3088)

–Matt J.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [October 28, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/activerecord-has-many-vs-has-one-scope/178004/4 "2009-10-28T15:28:59Z")

</div>

so, will we see this patch applied in 2.3.5?
