2

I'd like to ask a little question. It's rather trivial I guess but I might just look for the wrong solutions to my issue so I cant get it working.

I have a model called request.rb which has a method called self.dates

def self.dates
     from_date = Request.last.date.to_date 
     to_date = Request.last.todate.to_date 
     weekdays = (from_date..to_date).select { |d| (1..5).include?(d.wday)}.count
     weekend_days = (from_date..to_date).select { |d| [0, 6].include?(d.wday)}.count
end

I have another model called hotels.rb where I'd like to call the variables weekdays and weekend_days for the price calculation.

 def self.best_deal_nm
    weekday_nm_tot = (Request.dates.weekdays * pricea.wdpricenm) + (Request.dates.weekdays *  pricea.wepricenm)
    weekend_nm_tot = (Request.dates.weekend_days * priceb.wdpricenm) + (Request.dates.weekend_days * priceb.wepricenm)

    [weekday_nm_tot, weekend_nm_tot].min

  end

Unfortunately the code above doesnt work. My question is therefore how can I possibly call these two variables in my hotel model.

Thanks a lot for help

1 Answer 1

2

Just return all info in last line into your self.dates method like

def self.dates
 from_date = Request.last.date.to_date 
 to_date = Request.last.todate.to_date 
 weekdays = (from_date..to_date).select { |d| (1..5).include?(d.wday)}.count
 weekend_days = (from_date..to_date).select { |d| [0, 6].include?(d.wday)}.count
 {'from_date' => from_date, 'to_date' => to_date, 'weekdays' => weekdays, 'weekend_days' => weekend_days}
end

After call Request.dates from hotels.rb you could access to all variables added to hash.

weekday_nm_tot = (Request.dates['weekdays'] * pricea.wdpricenm) + (Request.dates['weekdays'] *  pricea.wepricenm)
Sign up to request clarification or add additional context in comments.

Comments

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.