我想问一个小问题。我觉得它相当微不足道,但我可能只是为我的问题寻找错误的解决方案,所以我无法让它发挥作用。
我有一个名为 request.rb 的模型,它有一个名为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
我有另一个名为 hotels.rb 的模型,我想在工作日和周末日调用变量进行价格计算。
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
不幸的是,上面的代码不起作用。因此,我的问题是如何在酒店模型中调用这两个变量。
非常感谢您的帮助
答案 0 :(得分:2)
只需将最后一行中的所有信息返回到您的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
{'from_date' => from_date, 'to_date' => to_date, 'weekdays' => weekdays, 'weekend_days' => weekend_days}
end
从Request.dates
致电hotels.rb
后,您可以访问添加到哈希的所有变量。
weekday_nm_tot = (Request.dates['weekdays'] * pricea.wdpricenm) + (Request.dates['weekdays'] * pricea.wepricenm)