# How to make an SQL querey Within a partial, is that possilbe?

**URL:** <https://www.ruby-forum.com/t/how-to-make-an-sql-querey-within-a-partial-is-that-possilbe/189725>\
**Category:** Rails\
**Created:** [June 6, 2010, 9:53am UTC](https://www.ruby-forum.com/t/how-to-make-an-sql-querey-within-a-partial-is-that-possilbe/189725 "2010-06-06T09:53:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![happysmiley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/df705f/32.png) [@happysmiley](https://www.ruby-forum.com/u/happysmiley)\
**Post date:** [June 6, 2010, 9:53am UTC](https://www.ruby-forum.com/t/how-to-make-an-sql-querey-within-a-partial-is-that-possilbe/189725/1 "2010-06-06T09:53:05Z")

</div>

Hello,  
I’m quite new to Rails and english is even not my first language, so  
please excused nescience and clerical mistake.  
I’ll try to explain my problem as good as possible.

I try to implement a page where a profil is shown, this is searched by  
ID.  
within this profil page I want to include the comments that refer to  
this profil.  
In the back I have to database tabels, the one “profiles” and a second  
“comments”.  
In the second one I ve the column “profil\_id” because my Idea was to  
search for the Id of the profil which is shown and than to display the  
comments in which the profil\_id is the same.

I already tried to implement this Idea but I hit some problems:

the profil page works fine.

show.erb.html:

  
Titel  
  

  
\<%= h @profil.title %\>  

  
  
  
  
  
  

  
\<%= h @profil.subtitle %\>  

  
  
  

```
         ......
         ......
         ....

        <div style="width: 500px; margin-top: 20px;">
<h2>Kommentare</h2>
<%= render :partial => "comments/

```

comments\_show", :locals=\>{:profil\_id=\>@profil.id} %\>

and there I tried to call a partial which display the comments…  
And that my problem…  
I hand over the profil\_id (which works fine) but I ve NO idea how and  
where to include the Search to find the comments with the same id…

in the file \_comments\_show.erb.html i ll tried something like that:

---

<div class="post-metadata">

**Author:** ![happysmiley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/df705f/32.png) [@happysmiley](https://www.ruby-forum.com/u/happysmiley)\
**Post date:** [June 6, 2010, 9:55am UTC](https://www.ruby-forum.com/t/how-to-make-an-sql-querey-within-a-partial-is-that-possilbe/189725/2 "2010-06-06T09:55:26Z")

</div>

I JUST MISS THE END , sorry  
Hello,  
I’m quite new to Rails and english is even not my first language, so  
please excused nescience and clerical mistake.  
I’ll try to explain my problem as good as possible.

I try to implement a page where a profil is shown, this is searched by  
ID.  
within this profil page I want to include the comments that refer to  
this profil.  
In the back I have to database tabels, the one “profiles” and a second  
“comments”.  
In the second one I ve the column “profil\_id” because my Idea was to  
search for the Id of the profil which is shown and than to display the  
comments in which the profil\_id is the same.

I already tried to implement this Idea but I hit some problems:

the profil page works fine.

show.erb.html:

  
Titel  
  

  
\<%= h @profil.title %\>  

  
  
  
  
  
  

  
\<%= h @profil.subtitle %\>  

  
  
  

```
         ......
         ......
         ....

        <div style="width: 500px; margin-top: 20px;">
            <h2>Kommentare</h2>
            <%= render :partial => "comments/

```

comments\_show", :locals=\>{:profil\_id=\>@profil.id} %\>

and there I tried to call a partial which display the comments…  
And that my problem…  
I hand over the profil\_id (which works fine) but I ve NO idea how and  
where to include the Search to find the comments with the same id…

in the file \_comments\_show.erb.html i ll tried something like that:  
\<%= h @comment.title %\>  
but that doesnt work.

Please help

---

<div class="post-metadata">

**Author:** ![happysmiley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/df705f/32.png) [@happysmiley](https://www.ruby-forum.com/u/happysmiley)\
**Post date:** [June 6, 2010, 11:54am UTC](https://www.ruby-forum.com/t/how-to-make-an-sql-querey-within-a-partial-is-that-possilbe/189725/3 "2010-06-06T11:54:51Z")

</div>

On Jun 6, 8:54 am, happysmiley [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In the back I have to database tabels, the one “profiles” and a second  
> “comments”.  
> In the second one I ve the column “profil\_id” because my Idea was to  
> search for the Id of the profil which is shown and than to display the  
> comments in which the profil\_id is the same.

You should have an association between profiles and comments (profile  
has\_one :comment or has\_many :comments) and then profile.comment or  
profile.comments will be the comment(s) you are looking for

Fred
