# How to fire distinct query?

**URL:** <https://www.ruby-forum.com/t/how-to-fire-distinct-query/145354>\
**Category:** Rails\
**Created:** [August 8, 2008, 11:02am UTC](https://www.ruby-forum.com/t/how-to-fire-distinct-query/145354 "2008-08-08T11:02:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dharmdip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@dharmdip](https://www.ruby-forum.com/u/dharmdip)\
**Post date:** [August 8, 2008, 11:02am UTC](https://www.ruby-forum.com/t/how-to-fire-distinct-query/145354/1 "2008-08-08T11:02:01Z")

</div>

i am having this code for record distinct but this code is not working  
???

* * *

\<%for postjob in @postjob = Postjob.find(:all)%\>

###### This Line of code is not working

\<%@postjob = Postjob.find( :all, :select =\> ‘DISTINCT country’ )%\>  
####### This part is working ##########

\<%= link\_to "#{postjob.country}" , :controller =\> "", :action =\> "country\_list" , :id =\> p%\>

\<%end%\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

please help !

---

<div class="post-metadata">

**Author:** ![dharmdip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@dharmdip](https://www.ruby-forum.com/u/dharmdip)\
**Post date:** [August 8, 2008, 11:16am UTC](https://www.ruby-forum.com/t/how-to-fire-distinct-query/145354/2 "2008-08-08T11:16:58Z")

</div>

Dharmdip R. wrote:

> i am having this code for record distinct but this code is not working  
> ???
> 
> * * *
> 
> \<%for postjob in @postjob = Postjob.find(:all)%\>
> 
> ###### This Line of code is not working
> 
> \<%@postjob = Postjob.find( :all, :select =\> ‘DISTINCT country’ )%\>  
> ####### This part is working ##########
> 
> \<%= link\_to "#{postjob.country}" , :controller =\> "", :action =\> "country\_list" , :id =\> p%\>
> 
> \<%end%\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
> 
> please help !

Just go with  
\<%for postjob in @postjob = Postjob.find(:all, :select =\> ‘DISTINCT  
country’) %\>  
ba bye

---

<div class="post-metadata">

**Author:** ![dharmdip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@dharmdip](https://www.ruby-forum.com/u/dharmdip)\
**Post date:** [August 8, 2008, 11:57am UTC](https://www.ruby-forum.com/t/how-to-fire-distinct-query/145354/3 "2008-08-08T11:57:20Z")

</div>

On Fri, Aug 8, 2008 at 11:02 AM, Dharmdip R. \<  
removed\_email\_address@domain.invalid\> wrote:

> \<%for postjob in @postjob = Postjob.find(:all)%\>

so at this point @postjob is an Array of your Postjobs

> ###### This Line of code is not working
> 
> \<%@postjob = Postjob.find( :all, :select =\> ‘DISTINCT country’ )%\>

and then here you’re re-assigning it something else.

> ####### This part is working ##########
> 
> \<%= link\_to "#{postjob.country}" , :controller =\> "", :action =\> "country\_list" , :id =\> p%\>

Where does p used in (:id =\> p) come from?

Maybe you can give more details about your Model and what  
exactly you would like to present using this code.

Franz

---

<div class="post-metadata">

**Author:** ![dharmdip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@dharmdip](https://www.ruby-forum.com/u/dharmdip)\
**Post date:** [August 8, 2008, 12:53pm UTC](https://www.ruby-forum.com/t/how-to-fire-distinct-query/145354/4 "2008-08-08T12:53:38Z")

</div>

hi,

U can also do like this,

@postjob= Postjob.find\_by\_sql(“select distinct from postjobs”)

thanks,  
shripad
