# ActiveRecord model relationship with YAML file

**URL:** <https://www.ruby-forum.com/t/activerecord-model-relationship-with-yaml-file/189434>\
**Category:** Rails\
**Created:** [May 31, 2010, 11:43pm UTC](https://www.ruby-forum.com/t/activerecord-model-relationship-with-yaml-file/189434 "2010-05-31T23:43:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [May 31, 2010, 11:43pm UTC](https://www.ruby-forum.com/t/activerecord-model-relationship-with-yaml-file/189434/1 "2010-05-31T23:43:10Z")

</div>

Hi,

I have an ActiveRecord model Group and a YAML table stored in  
config/users.yaml just like:

```
- {login: titi, password: 123456, group_id: 2}
- {login: toto, password: 987654, group_id: 7}

```

And I would like to link them by a sexy way. Can you help me?

I would be happy if I can do this (as example): User.first.group.id # =\>  
2

Many thanks for any help!

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [May 31, 2010, 11:58pm UTC](https://www.ruby-forum.com/t/activerecord-model-relationship-with-yaml-file/189434/2 "2010-05-31T23:58:54Z")

</div>

Paul A. wrote:

> Hi,
> 
> I have an ActiveRecord model Group and a YAML table stored in  
> config/users.yaml just like:
> 
> ```
> - {login: titi, password: 123456, group_id: 2}
> - {login: toto, password: 987654, group_id: 7}
> 
> ```
> 
> And I would like to link them by a sexy way. Can you help me?
> 
> I would be happy if I can do this (as example): User.first.group.id # =\>  
> 2
> 
> Many thanks for any help!

This will probably be easier in Rails 3, but not impossible in Rails 2.  
Which are you using?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [June 1, 2010, 12:03am UTC](https://www.ruby-forum.com/t/activerecord-model-relationship-with-yaml-file/189434/3 "2010-06-01T00:03:34Z")

</div>

Marnen Laibow-Koser wrote:

> This will probably be easier in Rails 3, but not impossible in Rails 2.  
> Which are you using?

Thank for your quick answer. I’m using Rails 3 😛

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [June 1, 2010, 1:22am UTC](https://www.ruby-forum.com/t/activerecord-model-relationship-with-yaml-file/189434/4 "2010-06-01T01:22:05Z")

</div>

Hmm, [GitHub - active-hash/active\_hash: A readonly ActiveRecord-esque base class that lets you use a hash, a Yaml file or a custom file as the datasource](http://github.com/zilkey/active_hash) looks very cool. And Rails 3  
ready. But it do not allow habtm relations :s

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [June 1, 2010, 10:19am UTC](https://www.ruby-forum.com/t/activerecord-model-relationship-with-yaml-file/189434/5 "2010-06-01T10:19:26Z")

</div>

On 31 May 2010 22:43, Paul A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would be happy if I can do this (as example): User.first.group.id # =\>  
> 2

You could run a rake task to put the data in the database, unless  
there is a good reason why you need it in a yaml file.

Colin

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [June 1, 2010, 8:34pm UTC](https://www.ruby-forum.com/t/activerecord-model-relationship-with-yaml-file/189434/6 "2010-06-01T20:34:57Z")

</div>

Colin L. wrote:

> On 31 May 2010 22:43, Paul A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I would be happy if I can do this (as example): User.first.group.id # =\>  
> > 2
> 
> You could run a rake task to put the data in the database, unless  
> there is a good reason why you need it in a yaml file.

I have 3 goods reasons for that:

- data access is faster
- read-only is sufficient
- it’s DRY (I don’t repeat data between rake task and database: the  
YAML file is my data)

I think Active Hash is awesome. Really awesome.
