# Quick question about a loop

**URL:** <https://www.ruby-forum.com/t/quick-question-about-a-loop/75469>\
**Category:** Rails\
**Created:** [November 10, 2006, 3:37am UTC](https://www.ruby-forum.com/t/quick-question-about-a-loop/75469 "2006-11-10T03:37:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 10, 2006, 3:37am UTC](https://www.ruby-forum.com/t/quick-question-about-a-loop/75469/1 "2006-11-10T03:37:29Z")

</div>

I’m writing a little recipe application, and I want my user profile  
pages to list that particular user’s recipes.

I can’t figure out how to have the code say “Display all of the recipes  
where the user\_id column equals the id of this particular user profile”.

I know this is probably simple, but I haven’t written many loops without  
copying off of other loops. ☹

Thanks!!

Dave

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 10, 2006, 3:53am UTC](https://www.ruby-forum.com/t/quick-question-about-a-loop/75469/2 "2006-11-10T03:53:10Z")

</div>

Hi –

On Fri, 10 Nov 2006, Dave A. wrote:

> I’m writing a little recipe application, and I want my user profile  
> pages to list that particular user’s recipes.
> 
> I can’t figure out how to have the code say “Display all of the recipes  
> where the user\_id column equals the id of this particular user profile”.

If you’ve done the “wiring” correctly (table and column names, model  
names and associations), you should be able to do something like:

\<% @user.recipes.each do |recipe| %\>  
… do stuff with recipe here …  
\<% end %\>

This is predicated on:

- a user\_id column in the recipes table

- this line in the user.rb model class:

- this line in the recipe.rb model class:

And the assumption is that whenever you create a recipe, you assign a  
user to it – like this:

recipe.user = user

or

user.recipes \<\< recipe

All of this is just the basic (but very powerful) mechanism by which  
ActiveRecord lets you use nice high-level Ruby commands, like  
@user.recipes, to manipulate database-level relationships.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 10, 2006, 3:57am UTC](https://www.ruby-forum.com/t/quick-question-about-a-loop/75469/3 "2006-11-10T03:57:11Z")

</div>

— models —

User \< ActiveRecord::Base

has\_many :recipes

end

Recipes \< ActiveRecord::Base

belongs\_to :user

end

— controller —

def user\_profile  
@user = User.find(params[:id]) # all prehaps get user from the  
session  
end

– view — (user\_profile.rhtml)

\<% @user.recipes.each do | recipe | -%\>  
\<%= h recipe.name %\>  
\<% end -%\>

HTH,  
Nicholas

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 10, 2006, 4:06am UTC](https://www.ruby-forum.com/t/quick-question-about-a-loop/75469/4 "2006-11-10T04:06:52Z")

</div>

Great, that was just what I was looking for. I had all of the “wiring”  
right, and I knew there was an easy way to do it! Thanks!!
