如何在 Ruby on Rails 中找到模型搜索的三个最佳匹配?

发布于 2024-11-13 06:14:51 字数 980 浏览 2 评论 0原文

我正在开发一个寻狗器来帮助人们选择最适合他们生活方式的狗品种。

因此,我使用 Scaffold 创建了狗(那么我们可以说我创建了狗模型吗?)

class CreateDogs < ActiveRecord::Migration
    def self.up
        create_table :dogs do |t|
            t.string :breed
            t.integer :speed
            t.integer :size
            t.integer :friendly
            t.timestamps
        end
    end
end
  • 速度 的评级范围为 1-10,其中 10 表示最快。
  • size 分为 1-10 级,其中 10 为最重。
  • 友好 评级为 1-10,其中 10 为最友好。

例如,品种:Golden Lab 的得分为

  • 速度 = 5
  • 大小 = 8
  • 友好 = 10

我正在创建一个表单,供用户填写他们正在寻找的属性范围,因此他们寻找

  • 速度 = 10
  • 大小 = 5
  • Friendly = 10

我如何使用 Ruby on Rails 返回搜索词的前三个品种匹配?

我一直在研究 <=> 运算符 sort_by,但我不确定如何将它们全部串在一起。起初我以为这是一个简单的搜索 - 但所有项目都需要相互比较。

另外,我怎样才能增加友善度的权重,使其在狗的选择方面比其他两个属性更重要?

我是 Ruby on Rails 的新手,如果我使用了不正确的术语,请原谅。

I'm building a dog finder to help people choose the best dog breed for their lifestyle.

So I have created the Dog using Scaffold (could we say have I created a Dog model then?)

class CreateDogs < ActiveRecord::Migration
    def self.up
        create_table :dogs do |t|
            t.string :breed
            t.integer :speed
            t.integer :size
            t.integer :friendly
            t.timestamps
        end
    end
end
  • speed is rated from 1-10 with 10 being fastest.
  • size is rated 1-10 with 10 being heaviest.
  • friendly is rated 1-10 with 10 being friendliest.

The breed: Golden Lab for instance has a score

  • speed = 5
  • size = 8
  • friendly = 10

I'm creating a form for users to fill in the range of attributes they are looking for, so they looking for a

  • speed = 10
  • size = 5
  • friendly = 10

How would I return the top three breed matches for their search terms, using Ruby on Rails?

I've been looking at the <=> operator, sort_by, but I'm not sure how to string it all together. I thought it was a simple search at first - but all items need to be compared with each other.

Also, how could I to add weighting to friendliness, so it's more important than the other two attributes in terms of dog selection?

I'm new to Ruby on Rails, so excuse me if I use incorrect terms.

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

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

发布评论

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

评论(2

咆哮 2024-11-20 06:14:51

你可以计算出查询值与实际值的差值,并取绝对值(即把所有负数都设为正值)来看看愿望与现实的接近程度:

abs(wanted_speed - speed)
abs(wanted_size - size)
abs(wanted_friendly - friendly)

然后将这些差值相加,看看查询值与实际值相差多少每只狗的实际价值。

要对任何属性赋予权重,只需在求和之前将其乘以某个值即可。

也许这是有效的:

@dogs = Dog.all(
  :order => ["ABS(speed - ?) + ABS(size - ?) + (ABS(friendly - ?) * 3)", 
             params[:speed], params[:size], params[:friendly]], 
  :limit => 3)

对于权重,您还可以在表单中添加变量(例如,重要= 3,不关心= 2,根本不重要= 1)并将这些变量添加到查询中,而不是硬编码 3..

You can calculate the difference between the queried and the actual value and get the absolute value (ie. make all negative numbers positive) to see how close the wish is to reality:

abs(wanted_speed - speed)
abs(wanted_size - size)
abs(wanted_friendly - friendly)

Then sum up these differences to see how much the queried values differ from the actual values of each dog.

To put weight on any attribute just multiply it with some value before sum up.

Maybe this is working:

@dogs = Dog.all(
  :order => ["ABS(speed - ?) + ABS(size - ?) + (ABS(friendly - ?) * 3)", 
             params[:speed], params[:size], params[:friendly]], 
  :limit => 3)

For the weight you could also add variables in your form (eg. important = 3, don't care = 2, not important at all = 1) and add these to the query instead of the hardcoded 3.

我很OK 2024-11-20 06:14:51
@dogs = Dog.all(:limit => 3, :order => "speed + size + friendly DESC")

如果您要添加权重,则可以添加乘数,例如,权重友好性将增加一倍,大小将增加三倍:

@dogs = Dog.all(:limit => 3, :order => "speed + (3 * size) + (2 * friendly) DESC")
@dogs = Dog.all(:limit => 3, :order => "speed + size + friendly DESC")

If you would add weighting, you can add multipliers, like this would weight friendlyness double and size triple:

@dogs = Dog.all(:limit => 3, :order => "speed + (3 * size) + (2 * friendly) DESC")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文