在我的Rails 4应用程序中,我开始将我的逻辑移动到模型上(工作在胖模型原理上)。然而,我有点不确定如何最好地解决基本问题。将逻辑移动到模型
我的应用程序显示登录用户的预订。在我BookingsController我有一个显示确认的订单的操作:
def confirmed
@bookings = Booking.where(:status => 3, :accommodation_id => current_user.accommodation.id).order('updated_at DESC')
end
不过,我曾试图逻辑移动到控制器,使得BookingsController现在看起来是这样的:
def confirmed
@bookings = Booking.confirmed_bookings
end
和预订模式是这样的:
def confirmed_bookings
bookings = where(:status => 3, :accommodation_id => current_user.accommodation.id).order('updated_at DESC')
end
我收到一个未定义的方法`confirmed_bookings'错误,不知道我在做什么错误。对此和一些简单的“初学者”建议的修复将非常感激。
Slicedpan有你的问题的答案,但也没有理由在你的方法局部变量'预订'。 – gregates
好的知道 - 所以我假设结果只是在代码完成时自动返回给控制器 - 不需要显式返回语句? – tommyd456
ruby方法总是返回最后一个评估表达式的值 - 在这种情况下,一个“ActiveRecord :: Relation”对象 – gregates