【发布时间】:2015-07-23 17:24:15
【问题描述】:
我正在尝试计算一个“用户”与另一个用户有多少个“提示”。 这是我这样做的方法:
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
我知道有些用户有他们都评价过的小费,但在我的表格中,所有用户的小费亲和度都是 0。
可能是什么问题?非常感谢任何帮助!
编辑:这是连接模型,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
我确实在尝试找到两个哈希的交集。这两个哈希是两个用户的提示,一个是self,一个是user。
再次感谢!
【问题讨论】:
-
有关您的数据结构的更多信息将与此处相关。
-
包含哪些内容会有帮助?
-
用户如何与提示相关联?另外值得注意的是,在 Ruby 变量名中使用下划线,如
tip_affinity,最后一行的return语句是隐含的,可以省略。 -
一个用户有_many 提示和一个提示有_many 用户。是这个意思吗?
-
大概是这样,是的,但这似乎很奇怪。你不应该有很多这样的。您需要在中间有一个连接模型。
标签: arrays ruby ruby-on-rails-3 hash