Rails:根据多个参数显示产品
我正在学习 Rails 并尝试建立一个产品库,其中的产品将根据三个元素进行显示:位置、类别和到期日期(产品可以有多个位置和类别,但只有一个到期日期)。只要产品尚未过期,就会显示产品,并且可以通过下拉菜单选择位置和类别。
我开始写这个问题时,在合并位置和类别选择标准时遇到了困难,我找到了解决方案,但非常感谢任何有关可以做得更好的帮助。
我通过连接使用 has_many 来创建产品、位置和类别之间的连接。
这是模型:
class Product < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
has_many :localizations
has_many :locations, :through => :localizations
end
class Categorization < ActiveRecord::Base
belongs_to :product
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :categorizations
has_many :products, :through => :categorizations
end
class Localization < ActiveRecord::Base
belongs_to :product
belongs_to :location
end
class Location < ActiveRecord::Base
has_many :localizations
has_many :products, :through => :localizations
end
这是我的控制器。地点及地点类别 ID 作为参数传递,并且产品的到期日期必须大于当前时间:
class LibraryController < ApplicationController
def index
@products = Product.find(:all, include => [ :locations, :categories ],
:conditions => ['expiry_date > ? AND locations.id = ? AND categories.id = ?',
Time.now, params[:location_id],params[:category_id]])
end
end
因此,通过在 URL 中传递 location_id 和 Category_id 参数,我可以通过两者的组合列出产品。
有没有更好的方法来实现我想要做的事情?
I'm learning rails and trying to set up a product library where the products will be displayed based on three elements: location, category and expiry date (products can have multiple locations and categories but just one expiry date). Products will be shown as long as their expiry date hasn't passed and location and category selection will be via dropdown menus.
I started writing this question while having difficulty with incorporating the location and category selection criteria which i found a solution to but any help on what could be done better is greatly appreciated.
I've used has_many through connections to create the connections between the products, location and categories.
Here's the models:
class Product < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
has_many :localizations
has_many :locations, :through => :localizations
end
class Categorization < ActiveRecord::Base
belongs_to :product
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :categorizations
has_many :products, :through => :categorizations
end
class Localization < ActiveRecord::Base
belongs_to :product
belongs_to :location
end
class Location < ActiveRecord::Base
has_many :localizations
has_many :products, :through => :localizations
end
Here's my controller. Location & category ID's are passed as params and the expiry date of the products must be greater than the current time:
class LibraryController < ApplicationController
def index
@products = Product.find(:all, include => [ :locations, :categories ],
:conditions => ['expiry_date > ? AND locations.id = ? AND categories.id = ?',
Time.now, params[:location_id],params[:category_id]])
end
end
So by passing the location_id and category_id params in the URL I can list products by a combination of both.
Is there a better way of achieving what I'm trying to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这也将实现您想要的功能:
您还可以使用
Product.where
,据说这比 find 更好。欲了解更多信息,请谷歌“动态查找器”。
This will also do what you want:
You can also user
Product.where
which is supposedly better than find.For more information, Google "dynamic finders".
好的。不,我认为在这种情况下没有“更好”的方法。当然有“不同”的方式来做你想做的事,但从表面上看,你所做的很好,并且不会尖叫“这段代码太糟糕了!”或任何东西。
这里很难回答建议/风格的问题,因为最终的答案是,“在网络上搜索其他人在你的情况下正在做什么,如果你的解决方案看起来传统/合乎逻辑,你自己评估/做出决定”,或者这类问题可以通过研究该主题的相关书籍来回答。
回答这样的定性问题几乎是不可能的,因为:
我要求你定义“更好”的原因主要是因为#1 - 除非你给我们一个你想要实现的具体结果,否则你所有的你会得到 (a) 充满意见的答案,而不是针对特定目标,或 (b) 只是采取不同的做事方式,可能对你有帮助,也可能没有帮助。因此,它们在实际中并不是很有用。
Ok. No, I don't think there is a "better" way in this case. There certainly are "different" ways of doing what you want, but on the face of it, what you're doing is fine, and it doesn't scream out "this code is terrible!" or anything.
Questions of advice/style are tough to answer here, because ultimately the answer to them is, "search the web for what other people are doing in your situation, and evaluate/make the decision yourself if your solution seems conventional/logical," or these kinds of questions are answered via study of relevant books on the topic.
It's nearly impossible to answer a qualitative question like this, because:
The reason I ask you to define "better" is primarily because of #1 - unless you give us a specific outcome you're trying to achieve, all you'll get are (a) answers that are full of opinion, and not directed toward a specific goal or (b) simply a different way of doing something which may or may not help you. Therefore, they aren't very useful in practical terms.
您还可以通过使用“Product.where”(优于 Rails 3.1 中的 find)来改进您的解决方案,并将它们转换为 Rails 中的named_scopes,并根据需要链接它们。
You could also improve upon your solution by using, "Product.where" (preferred over find in rails 3.1) and also turn them into named_scopes in Rails like and chain them as required.