# Correct way to build an AR object with multiple associations?

**URL:** <https://www.ruby-forum.com/t/correct-way-to-build-an-ar-object-with-multiple-associations/155930>\
**Category:** Rails\
**Created:** [December 18, 2008, 8:49am UTC](https://www.ruby-forum.com/t/correct-way-to-build-an-ar-object-with-multiple-associations/155930 "2008-12-18T08:49:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lisa\_Klein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3bc359/32.png) [@Lisa\_Klein](https://www.ruby-forum.com/u/Lisa_Klein)\
**Post date:** [December 18, 2008, 8:49am UTC](https://www.ruby-forum.com/t/correct-way-to-build-an-ar-object-with-multiple-associations/155930/1 "2008-12-18T08:49:57Z")

</div>

I have a Book resource that belongs both to a User and to an Isbn.  
What is the recommended way of building a new Book record in this type  
of situation?

class Book \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :isbn  
end

Currently I’m doing the following:

@isbn = Isbn.find(…)  
book = @isbn.books.build(…)  
book.user\_id = @user.id  
book.save

Is this the way it’s supposed to be done? I know that it’s preferred  
to interact with dependent resources through their associations. So  
adding the user\_id manually seems a bit clunky to me. But I’m not  
sure how else to do it!

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Lisa\_Klein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3bc359/32.png) [@Lisa\_Klein](https://www.ruby-forum.com/u/Lisa_Klein)\
**Post date:** [December 18, 2008, 11:00am UTC](https://www.ruby-forum.com/t/correct-way-to-build-an-ar-object-with-multiple-associations/155930/2 "2008-12-18T11:00:11Z")

</div>

Lisa Klein wrote:

> book.user\_id = @user.id
> 
> Is this the way it’s supposed to be done? I know that it’s preferred  
> to interact with dependent resources through their associations. So  
> adding the user\_id manually seems a bit clunky to me. But I’m not

Do

book.user = @user

instead then (ultimately it’s the same thing obviously).

---

<div class="post-metadata">

**Author:** ![Lisa\_Klein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3bc359/32.png) [@Lisa\_Klein](https://www.ruby-forum.com/u/Lisa_Klein)\
**Post date:** [December 18, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/correct-way-to-build-an-ar-object-with-multiple-associations/155930/3 "2008-12-18T14:38:39Z")

</div>

Oooh, that clarified a few things. Thanks Andrew!
