# Linking 3 Models with has\_many relationships

**URL:** <https://www.ruby-forum.com/t/linking-3-models-with-has-many-relationships/183969>\
**Category:** Rails\
**Created:** [February 23, 2010, 9:17am UTC](https://www.ruby-forum.com/t/linking-3-models-with-has-many-relationships/183969 "2010-02-23T09:17:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Trowbridge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@James\_Trowbridge](https://www.ruby-forum.com/u/James_Trowbridge)\
**Post date:** [February 23, 2010, 9:17am UTC](https://www.ruby-forum.com/t/linking-3-models-with-has-many-relationships/183969/1 "2010-02-23T09:17:10Z")

</div>

Hello, i’ve been searching for a solution to a problem i’ve come  
across and have not been able to find it yet. I’m working on a  
website that returns the closest delivery restaurants to you based on  
your location. I’ve completed this but i’ve begun adding a review  
process to the websites. My problem right now is trying to link the  
users, reviews, and restaurants models together.

Right now the relationship i’ve drawn up is

Restaurant - has\_many :reviews  
User - has\_many :reviews  
Review- belongs\_to :user  
belongs\_to :restaurant

Now this has got me far enough to be able to link them. Showing the  
reviews for a restaurant on a restaurant show page, and showing all  
of the reviews of a user on a type of control panel page. However i  
don’t think i implemented this correctly, because i am not able to  
destroy a review and have it be removed from both the restaurant and  
the user.

def create  
@review = @restaurant.reviews.build(params[:review])  
@user\_review = current\_user.reviews.build(params[:review])  
if (@review.save && @user\_review.save)  
redirect\_to restaurant\_path(@restaurant)  
else  
render :action =\> “new”  
end  
end

this creates two separate reviews, one for each model. fields for a  
review are::

r = Review.find(114)  
=\> #\<Review id: 114, user\_id: nil, restaurant\_id: 5, title: “pizza”,  
body: “fdsafds”, rating: 4, created\_at: “2010-02-23 05:06:18”,  
updated\_at: “2010-02-23 05:06:18”\>

I need to have a single entry contain both my user\_id and  
restaurant\_id but i’m not sure how to accomplish this. I’ve been  
reading about multi-model forms, has\_many :through, and a bunch of  
different plugins but i cant seem to relate it to my problem. Any  
help is greatly appreciated, thanks!

---

<div class="post-metadata">

**Author:** ![James\_Trowbridge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@James\_Trowbridge](https://www.ruby-forum.com/u/James_Trowbridge)\
**Post date:** [February 23, 2010, 9:39am UTC](https://www.ruby-forum.com/t/linking-3-models-with-has-many-relationships/183969/2 "2010-02-23T09:39:16Z")

</div>

> However i don’t think i implemented this correctly  
> You only need to add a slight modification to the create action so that  
> the same review is connected to both the user and the restaurant:

@review = @restaurant.reviews.build(params[:review])  
@review.user\_id = current\_user.id #hooks this review up to current\_user  
if (@review.save)  
#(…)

---

<div class="post-metadata">

**Author:** ![James\_Trowbridge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@James\_Trowbridge](https://www.ruby-forum.com/u/James_Trowbridge)\
**Post date:** [February 23, 2010, 9:44am UTC](https://www.ruby-forum.com/t/linking-3-models-with-has-many-relationships/183969/3 "2010-02-23T09:44:41Z")

</div>

Fixed code:

> def create  
> @review = @restaurant.reviews.build(params[:review])  
> @review.user = current\_user # FIXED HERE  
> if (@review.save) # AND HERE  
> redirect\_to restaurant\_path(@restaurant)  
> else  
> render :action =\> “new”  
> end  
> end

this creates two separate reviews, one for each model. fields for a

> review are:

It will do, you were building two reviews and then saving them both.  
You  
should create one and then save it.

Cheers,

Andy
