# Using searchlogic to retrieve ids, then rebuild object?

**URL:** <https://www.ruby-forum.com/t/using-searchlogic-to-retrieve-ids-then-rebuild-object/188939>\
**Category:** Rails\
**Created:** [May 20, 2010, 8:40pm UTC](https://www.ruby-forum.com/t/using-searchlogic-to-retrieve-ids-then-rebuild-object/188939 "2010-05-20T20:40:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [May 20, 2010, 8:40pm UTC](https://www.ruby-forum.com/t/using-searchlogic-to-retrieve-ids-then-rebuild-object/188939/1 "2010-05-20T20:40:38Z")

</div>

im giving searchlogic a try to combine named scopes easily  
only issue i have is it doesnt return distinct results  
ive worked around this with a named scope of grouped which selects  
only the id and groups by the id  
once ive got the ids, how can i quickly rebuild the  
object…find(:id=\>[@ids]…or something similar?

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [May 20, 2010, 9:25pm UTC](https://www.ruby-forum.com/t/using-searchlogic-to-retrieve-ids-then-rebuild-object/188939/2 "2010-05-20T21:25:42Z")

</div>

On Thu, May 20, 2010 at 11:38 AM, dan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> im giving searchlogic a try to combine named scopes easily  
> only issue i have is it doesnt return distinct results  
> ive worked around this with a named scope of grouped which selects  
> only the id and groups by the id  
> once ive got the ids, how can i quickly rebuild the  
> object…find(:id=\>[@ids]…or something similar?

What’s the actual code to your query that uses Searchlogic? This will  
help  
other to better assist you.

-Conrad

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [May 21, 2010, 1:45am UTC](https://www.ruby-forum.com/t/using-searchlogic-to-retrieve-ids-then-rebuild-object/188939/3 "2010-05-21T01:45:41Z")

</div>

fixed

changed my :grouped named\_scope to distinct:

named\_scope :distinct, :select =\> “distinct locations.\*”

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [May 20, 2010, 11:38pm UTC](https://www.ruby-forum.com/t/using-searchlogic-to-retrieve-ids-then-rebuild-object/188939/4 "2010-05-20T23:38:01Z")

</div>

#model location.rb  
named\_scope :grouped, :select =\> ‘locations.id’, :group =\>  
‘locations.id’  
has\_many :criterias, :through =\> :locations\_criterias

#controller locations\_controller.rb

# i havent even gotten to the paginate part yet

@locations = Location.criterias\_name\_or\_name\_like(@query).grouped  
#.paginate(:page =\> params[:page])

the @locations returns multiples of same result  
so i added the grouped named scope that only selects the id in order  
to only group on id  
at this point i need to rebuild my location object using the id  
returned by @locations
