需要一个自我。使用 Acts_as_list 的 Rails 模型上的方法
我对 Rails 还比较陌生...我需要在我的产品模型上编写一个 self.method 来查找每个位置的下一个产品。我在页面上显示 1 个产品,并想要列表中的下一个产品。
def self.next_product
product = Product. # current product.position +1
end
显然这是行不通的...我对写作方法仍然很陌生。有人吗?
我正在使用 Rbates Railscast 147 和 Acts_as_list,我需要弄清楚如何获得下一个产品,
非常感谢
I am still newer to Rails... I need to write a self.method on my Product model to find the next Product per position. I am showing 1 Product on a page, and want the next one in the list.
def self.next_product
product = Product. # current product.position +1
end
obviously this won't work... I am still new to writing methods. anyone?
I am using Rbates Railscast 147 and Acts_as_list, I need to figure out how to get the next product
thanks so much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
acts_as_list 已经添加了获取下一个和上一个项目的方法,它们称为 higher_item (position - 1) 和 lower_item (position + 1)。但是,它们是实例方法,而不是类方法 (self.),因为要在列表中查找“下一项”,您需要从一项(Product 类的实例)开始,而不是从 Product 类本身开始。
acts_as_list already adds methods for getting the next and previous items, they are called higher_item (position - 1) and lower_item (position + 1). However, they are instance methods, not class methods (self.) because to find "the next item" in a list, you need to start with an item (an instance of the Product class), not the Product class itself.