Selecting records using a range.

0 views
Skip to first unread message

Carson

unread,
Mar 30, 2008, 10:34:36 AM3/30/08
to MidWest.rb
I am trying to select products from a Products table with a Price
attribute falling within a range of prices as selected from a
PriceRange table. I show a code snipit (below) knowing that it
doesn't
work, but it should illustrate what I am trying to accomplish. I have
tried several things but I am now operating beyond my knowledge bank.

----------------------- snipit ---------------------------------


def list_products_within_price_range
@price_range = PriceRange.find(params[:id])
@title = @price_range.name
@product_pages, @products = paginate(:products,
:per_page => 6,
:order => 'vintner',
:conditions => ['price < ?',
@price_range.high],
:conditions => ['price > ?',
@price_range.low])
render :action => 'products'
end


--------------------- end snipit ------------------------------


Suggestions please!

Craig Demyanovich

unread,
Mar 30, 2008, 11:28:54 AM3/30/08
to midw...@googlegroups.com
No problems are obvious to me. Please share any error messages or failing tests/specs.

Craig

Mark Van Holstyn

unread,
Mar 30, 2008, 12:18:42 PM3/30/08
to midw...@googlegroups.com


On Sun, Mar 30, 2008 at 10:34 AM, Carson <E.Carson...@gmail.com> wrote:
----------------------- snipit ---------------------------------
 def list_products_within_price_range
   @price_range = PriceRange.find(params[:id])
   @title = @price_range.name
   @product_pages, @products = paginate(:products,
                                        :per_page => 6,
                                        :order => 'vintner',
                                        :conditions => ['price < ?',
@price_range.high],
                                        :conditions => ['price > ?',
@price_range.low])
   render :action => 'products'
 end
--------------------- end snipit ------------------------------

You are specifiying conditions twice...


@product_pages, @products = paginate(:products,
                                        :per_page => 6,
                                        :order => 'vintner',
                                        :conditions => ['price < ?', @price_range.high],
                                        :conditions => ['price > ?', @price_range.low])

Change that to...


@product_pages, @products = paginate(:products,
                                        :per_page => 6,
                                        :order => 'vintner',
                                        :conditions => ['price < ? AND price > ?', @price_range.high, @price_range.low])


--
Mark Van Holstyn, Partner / Software Developer
mvanh...@mutuallyhuman.com, (616) 706-6842
Mutually Human Software, http://mutuallyhuman.com

Ed Campbell

unread,
Mar 30, 2008, 8:49:59 PM3/30/08
to midw...@googlegroups.com
Well, the syntax of each :conditions statement works individually and the attributes "high" and "low" contain the proper values. However, the two :conditions statements won't work together to form a range. If I delete either statement, it works on one end of the range.
 
Carson

 

Carson

unread,
Mar 31, 2008, 12:51:03 AM3/31/08
to MidWest.rb
This worked and was a little more straight forward:

:conditions => ['price between ? and ?', @price_range.low,
@price_range.high])

Otherwise, its structurally similar to your answer, Mark.

Carson


On Mar 30, 10:18 am, "Mark Van Holstyn" <mvanhols...@gmail.com> wrote:
> mvanhols...@mutuallyhuman.com, (616) 706-6842
> Mutually Human Software,http://mutuallyhuman.com- Hide quoted text -
>
> - Show quoted text -

Daniel Morrison

unread,
Mar 31, 2008, 9:19:55 AM3/31/08
to midw...@googlegroups.com
In newer versions of Rails, (sorry, can't keep track), you can also
pass the range in.

:conditions => {:price => (@price_range.low..@price_range.high)}

which will construct an SQL BETWEEN statement.

-DM

--
Daniel Morrison
[i] Collective Idea
http://collectiveidea.com
349 Pine Avenue
Holland, MI 49423
616-990-0155
dan...@collectiveidea.com

> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google
> Groups "MidWest.rb" group.
> To post to this group, send email to midw...@googlegroups.com
> To unsubscribe from this group, send email to midwestrb-...@googlegroups.com
> For more options, v

Carson

unread,
Mar 31, 2008, 10:30:52 AM3/31/08
to MidWest.rb
NICE!

On Mar 31, 7:19 am, Daniel Morrison <dan...@collectiveidea.com> wrote:
> In newer versions of Rails, (sorry, can't keep track), you can also  
> pass the range in.
>
> :conditions => {:price => (@price_range.low..@price_range.high)}
>
> which will construct an SQL BETWEEN statement.
>
> -DM
>
> --
> Daniel Morrison
> [i] Collective Ideahttp://collectiveidea.com
> >> Mutually Human Software,http://mutuallyhuman.com-Hide quoted text -
>
> >> - Show quoted text -
> > --~--~---------~--~----~------------~-------~--~----~
> > You received this message because you are subscribed to the Google  
> > Groups "MidWest.rb" group.
> > To post to this group, send email to midw...@googlegroups.com
> > To unsubscribe from this group, send email to midwestrb-...@googlegroups.com
> > For more options, v- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages