I have a little Rails beginners question:
inside my Rails helper, I created method that I'm using to show a price in my view:
def price
pricea = Hotel.order(wdpricenm: :asc).first
priceb = Hotel.order(wepricenm: :asc).first
if (pricea.wdpricenm + priceb.wepricenm) < (priceb.wdpricenm + priceb.wepricenm)
return (pricea.wdpricenm + priceb.wepricenm)
else
return (priceb.wdpricenm + priceb.wepricenm)
end
<td><%= price %></td>
Its works without problems but I'd like to put the pricea / priceb variables (that store the queries) somewhere else in the rails application since I want to use them for other methods as well.
My Question is therefore: What would you suggest where to put those price variables in Rails and especially what variable types would you use?
Thanks for all replies in advance,
Cheers Rob
price, and call them any where you want to use them..That's good trick I can tell you..calc_priceaand onecalc_priceb.. Then shift the logic to them respectively into those.. so.. now call them inside your methodpriceto get the job done. And if you need to use the result of either of the new re-factored methods anywhere else.. call them from that place.. Don't go for any powerful variable like Global..w(e/d)pricenm, what you are returning from the queries, why you are conditionally returning price based onprice(a/b). With that, we should be able to advise on a better structure... but I'd start with creating more descriptive variables.