# Calling a default record for a has\_many association

**URL:** <https://www.ruby-forum.com/t/calling-a-default-record-for-a-has-many-association/185486>\
**Category:** Rails\
**Created:** [March 21, 2010, 11:30am UTC](https://www.ruby-forum.com/t/calling-a-default-record-for-a-has-many-association/185486 "2010-03-21T11:30:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Angelo\_Ashmore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@Angelo\_Ashmore](https://www.ruby-forum.com/u/Angelo_Ashmore)\
**Post date:** [March 21, 2010, 11:30am UTC](https://www.ruby-forum.com/t/calling-a-default-record-for-a-has-many-association/185486/1 "2010-03-21T11:30:51Z")

</div>

I have a Shop model that has\_many Zones (a Zone assigns different  
pricing to products). I want to save a default Zone for a Shop. This  
is what I have in my Shop class:

has\_one :default\_zone, :class\_name =\> “Zone”

After that, however, I am a bit lost. I know I can manually set  
shop.default\_zone\_id to a zone.id, but I would imagine a better system  
of doing this exists.

Thanks,  
Angelo, a desperate man

---

<div class="post-metadata">

**Author:** ![Angelo\_Ashmore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@Angelo\_Ashmore](https://www.ruby-forum.com/u/Angelo_Ashmore)\
**Post date:** [March 22, 2010, 8:29am UTC](https://www.ruby-forum.com/t/calling-a-default-record-for-a-has-many-association/185486/2 "2010-03-22T08:29:34Z")

</div>

Hiya,

Not sure what you are after really but your solution seems okay to me.  
If you are wondering how to then update it then:

Check here:

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001834)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

You can do things like:

# build a default zone (doesn’t auto-save)

shop.build\_default\_zone(:name =\> ‘Zone1’)

# create a default zone (auto-saves)

shop.create\_default\_zone(:name =\> ‘Zone2’)

# or assign an existing zone:

zone = Zone.find(3)  
shop.default\_zone = zone

That any help?

Luke

---

<div class="post-metadata">

**Author:** ![Angelo\_Ashmore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@Angelo\_Ashmore](https://www.ruby-forum.com/u/Angelo_Ashmore)\
**Post date:** [March 22, 2010, 11:44am UTC](https://www.ruby-forum.com/t/calling-a-default-record-for-a-has-many-association/185486/3 "2010-03-22T11:44:49Z")

</div>

I tried different variations of what you posted, and unfortunately,  
none of them worked.

Both shop.build\_default\_zone and shop.create\_default\_zone create a  
Zone record with the Shop ID correctly saved, but the Shop’s  
default\_zone\_id column isn’t updated; shop.default\_zone\_id remains  
blank.

This is basically what I have in seeds.rb:

shop = Shop.new  
shop.name = “Sample”  
shop.save

zone = shop.build\_default\_zone  
zone.name = “Default”

shop.save

I attempted adding “shop.default\_zone = Zone.first” at the end of  
seeds.rb, but, again, it did not work.

Any ideas? Perhaps I’m ordering my saves/builds incorrectly? I can’t  
get this seemingly simple thing to work.

Thanks for your help,  
Angelo
