浏览代码

Add method for retrieving triadic closures

master
Eugen Rochko 7 年前
父节点
当前提交
017350e0ea
共有 1 个文件被更改,包括 19 次插入0 次删除
  1. +19
    -0
      app/models/account.rb

+ 19
- 0
app/models/account.rb 查看文件

@@ -193,6 +193,25 @@ class Account < ApplicationRecord
nil
end

def triadic_closures(account, limit = 5)
sql = <<SQL
WITH first_degree AS (
SELECT target_account_id
FROM follows
WHERE account_id = ?
)
SELECT accounts.*
FROM follows
INNER JOIN accounts ON follows.target_account_id = accounts.id
WHERE account_id IN (SELECT * FROM first_degree) AND target_account_id NOT IN (SELECT * FROM first_degree) AND target_account_id <> ?
GROUP BY target_account_id, accounts.id
ORDER BY count(account_id) DESC
LIMIT ?
SQL

Account.find_by_sql([sql, account.id, account.id, limit])
end

def following_map(target_account_ids, account_id)
follow_mapping(Follow.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id)
end


正在加载...
取消
保存