开发者

Ruby create an object from a query string

开发者 https://www.devze.com 2022-12-20 02:12 出处:网络
I am using a new Rails project & an old established Oracle db using adapter: oracle_enhanced. Would like to be able to have dynamic searches against the db depending upon what info/field(s) is ava

I am using a new Rails project & an old established Oracle db using adapter: oracle_enhanced. Would like to be able to have dynamic searches against the db depending upon what info/field(s) is available to search against.

I am trying to get from

http://www.abc.com/order/show?acct_num=a123&po_num=789z

and create an object such as

class Order
  attr_reader :customer_name, :acct_num, :order_date, :po_num, :qty.
end

I have the paramaters from the request.query_parameters hashtable.

Now in case I am going down the wrong path what I want to do is use the query string and be able to find all orders where acct_num_in_model = acct_num_from_query_string and po_num_from_model = po_num_from_query_string etc. If something is empty such as po_number then return all that match the acc开发者_Python百科t_number and if acct_num empty all that match that po. I am expecting the following:

abc.com/order/show?acct_num=a123&po_num=789z

to return all orders with acct_num=a123 and po_num=789z

abc.com/order/show?acct_num=a123 to return all orders with acct_num = a123

abc.com/order/show?po_num=789z

to return all orders with po_num = 789z

abc.com/order/show?po_num=789z&qty=6

to return all orders with po_num = 789z & qty=6


Given you want to use rails, I suggest this:

QUERY_WHITELIST = ['po_num','qty','acct_num']
Order.find :all, :conditions => params.slice(*QUERY_WHITELIST)

or

QUERY_WHITELIST = ['po_num','qty','acct_num']
Order.all :conditions => params.slice(*QUERY_WHITELIST)

depending on your Rails version.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号