0

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

6
  • No variables. Refactor the code. Break the logic for two price query into 2 more methods. Call them inside price, and call them any where you want to use them..That's good trick I can tell you.. Commented Jun 15, 2014 at 18:39
  • its different queries > WDpricenm: :asc and the other WEpricenm: :asc - this is just a fragment of the real code that I want to refactor because I dont want to put those query variable in several methods ... looks ugly ... could you give me an example of your suggestion above? Commented Jun 15, 2014 at 18:45
  • Low vision power I have gained recently..... Lollz Commented Jun 15, 2014 at 18:46
  • Create one method calc_pricea and one calc_priceb.. Then shift the logic to them respectively into those.. so.. now call them inside your method price to 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.. Commented Jun 15, 2014 at 18:52
  • 1
    Your code is not clear. Could you explain w(e/d)pricenm, what you are returning from the queries, why you are conditionally returning price based on price(a/b). With that, we should be able to advise on a better structure... but I'd start with creating more descriptive variables. Commented Jun 15, 2014 at 18:52

3 Answers 3

4

Use scope: http://guides.rubyonrails.org/active_record_querying.html#scopes

Class Hotel < ActiveRecord:Base
  scope :pricea, -> { order(wdpricenm: :asc).first }
  scope :priceb, -> { order(wepricenm: :asc).first }
end
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot Jorge and Arkhitech, how can I get wdpricenm from :priceb where I ordered the table according to wepricenm asc in the method? is it something like Hotel.priceb(wdpricenm) ?
You would simply do Hotel.priceb.wdpricenm
1
Class Hotel < ActiveRecord:Base

  def self.pricea
    self.order(wdpricenm: :asc).first
  end

  def self.priceb
    self.order(wepricenm: :asc).first
  end

end

Now you are able to use:

pricea = Hotel.pricea
priceb = Hotel.priceb

If you need those prices available in different views you can do in the controllers:

Before_action :retrieve_prices, only: [:action1, :action2]

def retrieve_prices
  @pricea = Hotel.pricea
  @priceb = Hotel.priceb
end

This way this variables will be available only for the selected actions, and the methods can be reused anywhere.

Comments

0

EDIT: per comment

In the model:

Class Hotel < ActiveRecord::Base
  scope :weekday_min, -> { where(minimum(:wdpricenm)).first }
  scope :weekend_min, -> { where(minimum(:wepricenm)).first }

  def self.best_deal(weekdays, weekend_days)
    weekday_min_tot = (weekdays * weekday_min.wdpricenm) + (weekend_days * weekday_min.wepricenm)
    weekend_min_tot = (weekend_days * weekend_min.wdpricenm) + (weekend_days * weekend_min.wepricenm)

    [weekday_min_tot, weekend_min_tot].min
  end
end

In the controller:

@best_deal = Hotel.best_deal

Display total price in the view(no hint as to which hotel though):

<%= @best_deal %>

Note the lack of helpers.

ALSO NOTE: The best deal might not be either of these choices. What you need is to calculate the total for each hotel and then take the minimum of that.

You have a bit of a journey ahead of you picking up Rails. It's a big bite. I recommend the Hartl tutorial to start with. And don't forget to learn Ruby.

2 Comments

thanks seph, what i'm trying to achieve is to find out: if 'sum of weekdays' * pricea.withweekdays (table ordered by the cheapest weekday price) + 'sum of weekenddays' * pricea.withweekENDdays is < or > as 'sum of weekdays' * priceb.withweekdays (table ordered by the cheapest weekENDday price) + 'sum of weekenddays' * priceb.withweekENDdays
@RobBerlin - I modified the code according to your comment. This still might not be what you are after. See notes above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.