# Searching a Related model with a scope

**URL:** <https://www.ruby-forum.com/t/searching-a-related-model-with-a-scope/143035>\
**Category:** Rails\
**Created:** [July 14, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/searching-a-related-model-with-a-scope/143035 "2008-07-14T17:57:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![itsbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ee59a6/32.png) [@itsbs](https://www.ruby-forum.com/u/itsbs)\
**Post date:** [July 14, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/searching-a-related-model-with-a-scope/143035/1 "2008-07-14T17:57:50Z")

</div>

Hi, everybody!

I am trying to figure out how to add a related model to an anonymous  
scope to complete a search. Right now, I have this:  
if request.post?  
scope = Record.scoped({})  
scope = scope.conditions “color = ?”, params[:color] unless  
params[:color] == ‘–Select Color–’  
scope = scope.conditions “size = ?”, params[:size] unless  
params[:size] == ‘–Select Size–’  
scope = scope.conditions “shape = ?”, params[:shape] unless  
params[:shape] == ‘–Select Sample Rate–’  
scope = scope.conditions “quantity = ?”, params[:quantity]  
unless params[:quantity] == ‘–Select Quantity–’  
scope = scope.conditions “file\_short = ?”, params[:length]  
unless params[:length] == ‘–Select Length–’  
scope = scope.conditions “pair.run.widget = ?”, params[:widget]  
unless params[:widget] == ‘–Select Name–’  
@records = scope  
end

Records belong to Pairs, and Pairs belong to Runs. Everything will  
work fine unless a widget is selected, and I need to have it located,  
at which point it returns a “No such column” from SQL. Is there a  
work around to allow you to get to a related model?
