2
我正面临着棘手的挑战。让我解释一下我想要实现的目标。如果用户使用Facebook登录我的应用程序,我会刮掉他们所有的Facebook好友UID并将其作为用户的facebook_friends存储。然后,一旦登录,用户就会看到即将到来的事件列表,并且如果任何与会者匹配用户的Facebook好友的UID并向他们强调此事件,我想检查每个事件。更好的方式来比较Rails中的两个数据集?
我选择如下创建Event.rb型号:
class Event < ActiveRecord::Base
# id :integer(11)
has_many :attendances, as: :attendable
has_many :attendees
def which_facebook_friends_are_coming_for(user)
matches = []
self.attendees.each do |attendee|
matches << user.facebook_friends.where("friend_uid=?", attendee.facebook_id)
end
return matches
end
end
你可以看到,我创建了which_facebook_friends_are_coming_for(用户)方法,但它给我的印象incredibily效率低下。当我从控制台运行它时,它确实有效,但如果我尝试以任何形式(如YAML)将其转储,我会被告知无法转储匿名模块。我假设这是因为现在'比赛'持有人不是一个班级(当它应该是FacebookFriends)。
必须有更好的方法来做到这一点,我很乐意提出一些建议。
为基准,其他类看起来是这样的:
class User < ActiveRecord::Base
# id :integer(11)
has_many :attendances, foreign_key: :attendee_id, :dependent => :destroy
has_many :facebook_friends
end
class FacebookFriend < ActiveRecord::Base
# user_id :integer(11)
# friend_uid :string
# friend_name :string
belongs_to :user
end
class Attendance < ActiveRecord::Base
# attendee_id :integer(11)
# attendable_type :string
# attendable_id :integer(11)
belongs_to :attendable, polymorphic: true
belongs_to :attendee, class_name: "User"
end
辉煌 - 谢谢您! – idrysdale