存储图像类别的最佳逻辑

发布于 2024-07-29 18:42:58 字数 382 浏览 4 评论 0原文

我正在创建一个系统,用户可以选择四个选项的任意组合。

然后根据它们的精确组合显示结果。

例如,如果用户选择颜色、文本和徽标。 仅返回具有颜色、文本和徽标的结果。

另一方面,我正在创建最终将返回的结果的“模板”。 每个模板都将被指定为当用户选择与该模板相对应的组合时能够正确返回。

我的问题:

在后端对这些信息进行分类以便可以根据用户请求提取的最佳方法是什么?

例如,我有一个模板,可以是颜色和文本或颜色、文本和徽标。 我的猜测是将这两者分成一个系列,然后在进行组合时,查询将审查每个系列以查找匹配的组合; 如果为 true,则返回特定的组合变体。

你会采取不同的做法吗?

谢谢!

I am creating a system where a user can select any combination of four options.

Then results are displayed based on their exact combination.

For example, if a user chose color, text, and logo. Only results featuring color, text, and logo would return.

On the other side, I am creating "templates" that will eventually be the results returned. Each template will be earmarked to be able to return properly when a user selects a combination that corresponds with the template.

My Question :

What is the best way to categorize this information in the back-end so it can be pulled by the user requests?

For example, I have a template that can be color and text OR color, text, and logo. My guess is two group these two in a family, and then when a combination is made, a query reviews each family for a matching combo; if true, that specific combo variation is returned.

Would you do it any differently?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

甲如呢乙后呢 2024-08-05 18:42:58

您不应该对可用类别的数量进行硬编码(如果明天您需要另一个类别怎么办?)

Combination
  has_many :categories
  belongs_to :template

  def self.find_by_selected_categories(array_of_categories)
    self.find(:first, :conditions => {:categories => array_of_categories})
  end
end

Template
  has_many: combinations
end

...

这种方法的缺点是您必须有一个对应表(类别组合)。

You shoudn't hardcode the number of available categories (what if tomorrow you need another category?)

Combination
  has_many :categories
  belongs_to :template

  def self.find_by_selected_categories(array_of_categories)
    self.find(:first, :conditions => {:categories => array_of_categories})
  end
end

Template
  has_many: combinations
end

...

The downside of this approach is that you must have a correspondence table (categories-combinations).

同展鸳鸯锦 2024-08-05 18:42:58

这是一个相当复杂的问题描述。 在没有真实的东西可供查看的情况下,我在解析其中一些内容时遇到了一些麻烦 - 例如“当进行组合时”。 (它与存储图像有什么关系?你的模板是图像吗?)

也就是说......我想说最简单的方法是将你的搜索作为 Rails 任务而不是数据任务来处理。 只需设置您的模板模型所需的任何属性:

# In the migration
create_table templates do |t|
  t.string   :color  # Set a validation for your hex codes, or whatever, in Rails
  t.string   :text
  t.string   :logo_file_name    # These all assume you're using the Paperclip gem.
  t.string   :logo_content_type # (http://thoughtbot.com/projects/paperclip)
  t.integer  :logo_file_size    # If you're tracking your logo attachment some other
  t.datetime :logo_updated_at   # way, do whatever's needed in that case.
end

然后,在模型中,为各种选项设置命名范围:

class Template < ActiveRecord::Base
  has_attached_file :logo

  named_scope :with_color, :conditions => {"color is not null"}
  named_scope :with_text, :conditions => {"text is not null"}
  named_scope :with_logo, :conditions => {"logo_file_name is not null"}
  # You could add :without_ named scopes too, of course, if you needed them
end

然后您可以简单地将它们链接在一起以匹配用户在搜索中检查的任何内容,例如 Template.with_color。 with_text.with_logo,您将获得命名范围过滤结束时剩余的内容。

那有意义吗? 命名范围对于这类事情非常方便——如果您以前没有遇到过它们,您可能需要 Google 一下它们。

This is a fairly complicated problem description. I'm having a little trouble parsing some of it -- e.g. "when a combination is made" -- in the absence of a real thing to look at. (And what does it have to do with storing images? Are your templates images?)

That said... I'd say the simplest approach is to handle your searching as a Rails task rather than a data task. Just set whatever attributes you need for your Template model:

# In the migration
create_table templates do |t|
  t.string   :color  # Set a validation for your hex codes, or whatever, in Rails
  t.string   :text
  t.string   :logo_file_name    # These all assume you're using the Paperclip gem.
  t.string   :logo_content_type # (http://thoughtbot.com/projects/paperclip)
  t.integer  :logo_file_size    # If you're tracking your logo attachment some other
  t.datetime :logo_updated_at   # way, do whatever's needed in that case.
end

Then, in the model, set named scopes for the various options:

class Template < ActiveRecord::Base
  has_attached_file :logo

  named_scope :with_color, :conditions => {"color is not null"}
  named_scope :with_text, :conditions => {"text is not null"}
  named_scope :with_logo, :conditions => {"logo_file_name is not null"}
  # You could add :without_ named scopes too, of course, if you needed them
end

Then you can simply chain them together to match whatever the user checked in their search, e.g. Template.with_color.with_text.with_logo and you'll get whatever survives at the end of the named scope filtering.

Does that make sense? Named scopes are very handy for this sort of thing -- you might want to Google on them if you haven't come across them before.

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