# Creating a new object with multiple associations

**URL:** <https://www.ruby-forum.com/t/creating-a-new-object-with-multiple-associations/73240>\
**Category:** Rails\
**Created:** [October 23, 2006, 5:46am UTC](https://www.ruby-forum.com/t/creating-a-new-object-with-multiple-associations/73240 "2006-10-23T05:46:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea5d25/32.png) [@eleo](https://www.ruby-forum.com/u/eleo)\
**Post date:** [October 23, 2006, 5:46am UTC](https://www.ruby-forum.com/t/creating-a-new-object-with-multiple-associations/73240/1 "2006-10-23T05:46:13Z")

</div>

This is a pretty simple question but I haven’t heard an answer for it  
yet.

Let’s say that a Bid object belongs\_to both an Auction and a User at  
the same time. To create a new object I could do something like  
user.bids.build which would associate the new bid with that user, or  
auction.bids.build which would associate the new Bid with the Auction,  
but I have no idea how to create a new bid that would be associate with  
both at the same time. I can of course force-feed the new bid object  
with the id of the Auction or User objects, but that is the stupid way  
to do it.

This is probably a very newb question, but it never came up with me  
before, and I don’t remember reading about it anywhere.

---

<div class="post-metadata">

**Author:** ![eleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea5d25/32.png) [@eleo](https://www.ruby-forum.com/u/eleo)\
**Post date:** [October 23, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/creating-a-new-object-with-multiple-associations/73240/2 "2006-10-23T16:42:25Z")

</div>

Eleo wrote:

> Let’s say that a Bid object belongs\_to both an Auction and a User at  
> the same time. To create a new object I could do something like  
> user.bids.build which would associate the new bid with that user, or  
> auction.bids.build which would associate the new Bid with the Auction,  
> but I have no idea how to create a new bid that would be associate with  
> both at the same time. I can of course force-feed the new bid object  
> with the id of the Auction or User objects, but that is the stupid way  
> to do it.

Rather than explictly setting the Bid object’s foreign key, either  
add the Bid object to the collection or assign the associated object:

@auction.bids \<\< @user.bids.build(params[:bid])

or

@bid = @user.bids.build(params[:bid])  
@bid.auction = @auction  
@bid.save!

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![eleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea5d25/32.png) [@eleo](https://www.ruby-forum.com/u/eleo)\
**Post date:** [October 23, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/creating-a-new-object-with-multiple-associations/73240/3 "2006-10-23T22:38:46Z")

</div>

Hmm I guess that will work. I like the first example because it uses  
less lines.

---

<div class="post-metadata">

**Author:** ![eleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea5d25/32.png) [@eleo](https://www.ruby-forum.com/u/eleo)\
**Post date:** [October 24, 2006, 12:13am UTC](https://www.ruby-forum.com/t/creating-a-new-object-with-multiple-associations/73240/4 "2006-10-24T00:13:09Z")

</div>

On 10/24/06, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > to do it.  
> > @bid.save!

I’d personally prefer something more semantically meaningful, thus  
delegating the creation and association of a bid to the model domain:

class User \< …

def bids\_on(auction, bid\_params)  
bid = Bid.new(bid\_params)  
bid.auction = auction  
bid.user = self  
bid.save!  
end

end

And then in your controller:

@user.bids\_on(auction, params[:bid])

This makes it immediately obvious what’s going on, and the  
implementation details are neatly encapsulated in the model.

Cheers,  
Max
