I am trying to compute how many 'tips' a 'user' has in common with another user. Here is my method doing so:
我正在尝试计算“用户”与其他用户共有的“提示”数量。这是我的方法:
class User < ActiveRecord::Base
has_many :tips, :inverse_of => :user
...
public
def tip_affinity_with(user)
tipAffinity = 0
user.tips.each do |tip|
if self.tips.include?(tip)
tipAffinity = tipAffinity + 1
end
end
return tipAffinity
end
end
I know that some users have tips that they have both rated, but in my table, the tipAffinity is 0 for all of the users.
我知道有些用户有提示他们都有评价,但在我的表中,tipAffinity为0表示所有用户。
What could be the problem? Any help is greatly appreciated!
可能是什么问题呢?任何帮助是极大的赞赏!
EDIT: Here is the join model, Affinity
:
编辑:这是连接模型,亲和力:
class Affinity < ActiveRecord::Base
attr_accessible :user_A_id, :user_B_id, :tips_value, :tips_valid, :profile_value, :profile_valid, :value
belongs_to :users
belongs_to :inverse_user, :class_name => "Users"
validates :tips_value, presence: true
validates :profile_value, presence: true
validates :user_A_id, presence: true
validates :user_B_id, presence: true
before_update :set_value
before_create :set_value
private
def set_value
self.value = 0.7*self.tips_value + 0.3*self.profile_value
#self.value = PredictionConfigs.find(1)*self.tips_value + (1 - PredictionConfigs.find(1))*self.profile_value #Use prediction configs
end
end
I am indeed trying to find the intersection of two hashes. The two hashes are the tips of two users, one is self
, the other is user
.
我确实试图找到两个哈希的交集。两个哈希是两个用户的提示,一个是自己,另一个是用户。
Thanks again!
2 个解决方案
#1
Although this is brutally inefficient, you might try:
虽然这是非常低效的,但您可以尝试:
(user.tips & self.tips).length
You really want to avoid loading models if you're not using the data contained within them. This should be possible to compute using only what's present in the database. Something like:
如果您没有使用其中包含的数据,您真的想避免加载模型。这应该可以仅使用数据库中存在的内容进行计算。就像是:
(user.tip_ids & self.tip_ids).length
#2
If your models are set up correctly, this can be done in the database with:
如果您的模型设置正确,可以在数据库中执行以下操作:
def tip_affinity_with(user)
user.tips.where(id: tip_ids).count
end
It seems to me based on the comments (and your example code) that your models may not be set up correctly. Do you have a join model between users and tips? Something like:
在我看来,根据评论(和您的示例代码),您的模型可能无法正确设置。您是否有用户和提示之间的联接模型?就像是:
class User < ActiveRecord::Base
has_many :suggestions
has_many :tips, through: :suggestions
end
class Suggestion < ActiveRecord::Base
belongs_to :user
belongs_to :tip
end
class Tip < ActiveRecord::Base
has_many :suggestions
has_many :users, through: :suggestions
end
It'd help to see your Tip
model. My guess is that Tip
belongs_to :user
and that's why you aren't getting any overlap between users, but I could be wrong.
看看你的Tip模型会有所帮助。我的猜测是Tip belongs_to:user,这就是为什么你没有在用户之间得到任何重叠,但我可能是错的。
Here's some more reading from the Rails guides on has_many :through
associations.
以下是关于has_many的Rails指南的更多内容:通过关联。
#1
Although this is brutally inefficient, you might try:
虽然这是非常低效的,但您可以尝试:
(user.tips & self.tips).length
You really want to avoid loading models if you're not using the data contained within them. This should be possible to compute using only what's present in the database. Something like:
如果您没有使用其中包含的数据,您真的想避免加载模型。这应该可以仅使用数据库中存在的内容进行计算。就像是:
(user.tip_ids & self.tip_ids).length
#2
If your models are set up correctly, this can be done in the database with:
如果您的模型设置正确,可以在数据库中执行以下操作:
def tip_affinity_with(user)
user.tips.where(id: tip_ids).count
end
It seems to me based on the comments (and your example code) that your models may not be set up correctly. Do you have a join model between users and tips? Something like:
在我看来,根据评论(和您的示例代码),您的模型可能无法正确设置。您是否有用户和提示之间的联接模型?就像是:
class User < ActiveRecord::Base
has_many :suggestions
has_many :tips, through: :suggestions
end
class Suggestion < ActiveRecord::Base
belongs_to :user
belongs_to :tip
end
class Tip < ActiveRecord::Base
has_many :suggestions
has_many :users, through: :suggestions
end
It'd help to see your Tip
model. My guess is that Tip
belongs_to :user
and that's why you aren't getting any overlap between users, but I could be wrong.
看看你的Tip模型会有所帮助。我的猜测是Tip belongs_to:user,这就是为什么你没有在用户之间得到任何重叠,但我可能是错的。
Here's some more reading from the Rails guides on has_many :through
associations.
以下是关于has_many的Rails指南的更多内容:通过关联。