Rails:根据多个参数显示产品

发布于 2024-12-05 03:38:18 字数 1451 浏览 0 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的往事 2024-12-12 03:38:18

这也将实现您想要的功能:

@products = Product.find_all_by_category_id_and_location_id(params[:category_id], params[:location_id])

您还可以使用 Product.where ,据说这比 find 更好。

欲了解更多信息,请谷歌“动态查找器”。

This will also do what you want:

@products = Product.find_all_by_category_id_and_location_id(params[:category_id], params[:location_id])

You can also user Product.where which is supposedly better than find.

For more information, Google "dynamic finders".

流星番茄 2024-12-12 03:38:18

好的。不,我认为在这种情况下没有“更好”的方法。当然有“不同”的方式来做你想做的事,但从表面上看,你所做的很好,并且不会尖叫“这段代码太糟糕了!”或任何东西。

这里很难回答建议/风格的问题,因为最终的答案是,“在网络上搜索其他人在你的情况下正在做什么,如果你的解决方案看起来传统/合乎逻辑,你自己评估/做出决定”,或者这类问题可以通过研究该主题的相关书籍来回答。

回答这样的定性问题几乎是不可能的,因为:

  1. 解决每个问题都有多种方法,其中许多方法既不是“正确”也不是“错误”
  2. 总是存在人们违反“规则”的边缘情况,在这种情况下甚至是非常规的解决方案绝对是做某事的最佳方式
  3. 。您是开发人员,是构建该事物的人。在某种程度上,你应该发挥领导作用,并决定什么是最好的。

我要求你定义“更好”的原因主要是因为#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:

  1. There's several ways to solve every problem, many of which are neither "right" or "wrong"
  2. There's always edge cases where people break the "rules", in which case even unconventional solutions can absolutely be the best way to do something
  3. You're the developer, the one building the thing. To some extent you're expected to take a leadership role, and decide what's best

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.

夏了南城 2024-12-12 03:38:18

您还可以通过使用“Product.where”(优于 Rails 3.1 中的 find)来改进您的解决方案,并将它们转换为 Rails 中的named_scopes,并根据需要链接它们。

scope :not_expired, where('expiry_date > ?', Time.now)

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.

scope :not_expired, where('expiry_date > ?', Time.now)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文