Ruby Rails - Snippets from a project that don't seem so eloquent I'm brand new to both Ruby and Rails, and while all the following works, it seems a bit messy. 

This is on a classified model to generate WHERE/AND WHERE clauses based on whether or not the user supplies a value through a dropdown. I'm not sure if there's a more Active Recordy way of doing this:

class Classified &lt; ActiveRecord::Base
   attr_accessible :car_id, :owner_id, :owner, :price, :condition, :description, :mileage
   belongs_to :car
   belongs_to :owner, :polymorphic =&gt; true
   has_one :model, :through =&gt; :car
   has_one :make, :through =&gt; :model
   has_many :pictures
 scope :filter_active_cars, lambda { |options| 
 filter = {}
 filter[:cars] = {:year =&gt; options[:year]} unless options[:year].empty?
 filter[:makes] = {:id =&gt; options[:make]} unless options[:make].empty?
 filter[:models] = {:id =&gt; options[:model]} unless options[:model].empty?

 joins(:car, :model, :make) 
 .where(filter)
 .includes(:car, :model, :make)
 }


The view has this

=select_tag('car[year]', options_for_select(years, :selected =&gt; @selected_year))
=collection_select(:car, :make, Make.all, :id, :name, {:include_blank =&gt; '- Make -', :selected =&gt; @selected_make}) 
=collection_select(:car, :model, @selected_make ? Model.where(:make_id =&gt; @selected_make) : Model.all, :id, :name, {:include_blank =&gt; '- Model -', :selected =&gt; @selected_model}) 


This is the way I'm passing the "select state" on the classified view. The controller has this snippet in the index action. This is so when a user filters a result and submits the form, the corresponding select box has the same value for the form helpers above.

if params[:car]
  params[:car].each do |option, value|
    next if value.empty?
    eval("@selected_#{option} = #{value}")
  end
end


On the form partial to generate and edit a classified. Obviously the chaining of &amp;&amp;s sucks, but it's the only way to get the default selection on new, edit, and when a user submits a new form that contains errors.

=select_tag('car[year]', options_for_select(years, :selected =&gt; @classified.car.nil? ? nil : @classified.car.year), :prompt =&gt; "- Year -")
=collection_select(:make, :id, Make.all, :id, :name, {:prompt =&gt; '- Make -', :selected =&gt; (@classified.car &amp;&amp; @classified.car.make &amp;&amp; @classified.car.make.try(:id))}) 
=collection_select(:car, :model_id, Model.all, :id, :name, {:include_blank =&gt; '- Model -', :selected =&gt; (@classified.car &amp;&amp; @classified.car.model &amp;&amp; @classified.car.model.try(:id))})
Maybe you can convert this:

scope :filter_active_cars, lambda { |options| 
 filter = {}
 filter[:cars] = {:year =&gt; options[:year]} unless options[:year].empty?
 filter[:makes] = {:id =&gt; options[:make]} unless options[:make].empty?
 filter[:models] = {:id =&gt; options[:model]} unless options[:model].empty?

 joins(:car, :model, :make) 
 .where(filter)
 .includes(:car, :model, :make)
 }
}


to this:

scope :filter_active_cars, lambda do |options| 
 with_option(:car, 'cars.year', :year).
 with_option(:make, 'makes.id', :make).
 with_option(:model, 'models.id', :model)
end

scope :with_option, lambda do |assoc, column, option|
  (options[option].present? ?
    joins(assoc).where(column =&gt; options[option]) :
    self).
  includes(assoc)
end


You can reuse the with_option scope in other models