Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在ActiveRecord中优化此查询-朋友提供的最新10个主题_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 如何在ActiveRecord中优化此查询-朋友提供的最新10个主题

Ruby on rails 如何在ActiveRecord中优化此查询-朋友提供的最新10个主题,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,用户型号: user_id phone_no ------- -------- 1 555-0001 2 555-0002 . . user_id phone_no ------- -------- user 1 555-0002 user 1 555-0003 user 2 555-0001 user 2 555-0004 topic_id user_id title -------- ---

用户
型号:

 user_id  phone_no
 -------  --------
   1      555-0001
   2      555-0002
   .
   .
 user_id  phone_no
 -------  --------
  user 1  555-0002
  user 1  555-0003
  user 2  555-0001
  user 2  555-0004
  topic_id  user_id  title
  --------  -------  -----
      1        1     Hello
      2        1     Hi
每个用户都有自己的电话号码

Friend
型号:

 user_id  phone_no
 -------  --------
   1      555-0001
   2      555-0002
   .
   .
 user_id  phone_no
 -------  --------
  user 1  555-0002
  user 1  555-0003
  user 2  555-0001
  user 2  555-0004
  topic_id  user_id  title
  --------  -------  -----
      1        1     Hello
      2        1     Hi
用户可能有许多电话号码。电话号码匹配被认为是朋友(一个方向),例如“代码> >55~90003 < /代码>的所有者是<代码>用户1 的朋友,反之亦然。

主题
型号:

 user_id  phone_no
 -------  --------
   1      555-0001
   2      555-0002
   .
   .
 user_id  phone_no
 -------  --------
  user 1  555-0002
  user 1  555-0003
  user 2  555-0001
  user 2  555-0004
  topic_id  user_id  title
  --------  -------  -----
      1        1     Hello
      2        1     Hi
如何优化用户好友获取最新10个主题的查询

我试过这样的方法:

user = User.find(2) # any user
phones = Friend.all(:conditions=>['user_id = ?',user.id],:select=>'phone_no').map {|x| x.phone_no}
friends = User.all(:conditions=>['phone_no in (?), phones]).map {|x| x.user_id}
topics = Topic.all(:conditions=>['user_id in (?), friends], :limit => 10, :order => 'updated_at DESC')
但在分解查询时担心性能。如何优化此ActiveRecord查询

class User
  has_many :topics
  has_many :friends
  has_many :friend_users, :through => :friends
  has_many :friend_topics, :through => :friend_users, :source => :topics      
end

class Friend
  belongs_to :user
  belongs_to :friend_user, :class_name => "User", :foreign_key => :phone_no, 
                :primary_key  => :phone_no
end

class Topic
  belongs_to :user
end
现在,给定一个用户,您可以按如下方式获取好友主题:

current_user.friend_topics.limit(10)
一定要加上

  • User
    类中的
    phone\u no
    列上的唯一索引
  • Friend
    类中的
    phone\u no
    列上的索引

关于“手机上的唯一索引\u no col”,如果我在
用户
模型中保留
手机\u no
col可选,那么索引不唯一会有什么副作用?我想应该可以。确保在
Friend
模型中的
phone\u no
col上添加非空约束。