3

以下を考えるとTweet

# db/schema.rb
create_table "tweets", force: true do |t|
  t.integer  "project_id",            null: false
  t.integer  "author_id",             null: false
  t.integer  "twitter_id",            limit: 8, null: false
  t.text     "text"
  t.integer  "in_reply_to_status_id", limit: 8
  t.integer  "previous_tweet_ids",    limit: 8, array: true
end

add_index "tweets", ["previous_tweet_ids"], name: "index_tweets_on_previous_tweet_ids", using: :gin
add_index "tweets", ["project_id", "author_id"], name: "index_tweets_on_project_id_and_author_id", using: :btree
add_index "tweets", ["project_id", "twitter_id"], name: "index_tweets_on_project_id_and_twitter_id", unique: true, using: :btree
add_index "tweets", ["project_id"], name: "index_tweets_on_project_id", using: :btree

previous_tweet_idstwitter_id以前のすべてのツイートを格納する Postgres 配列です。一連のツイートの以前のツイートを効率的に取得する方法を探しています。これは私が現在やっていることです:

# app/models/tweet.rb
class Tweet
  def previous_tweets
    @previous_tweets ||= project.tweets.where(twitter_id: previous_tweet_ids)
  end

  def future_tweets
    @future_tweets ||= project.tweets.where('previous_tweet_ids && ARRAY[?]', twitter_id)
  end
end

これらprevious_tweetsfuture_tweetsメソッドを ActiveRecord アソシエーションに変換して、ActiveRecord の熱心な読み込みを使用する方法はありTweet.incoming.includes(:previous_tweets, :future_tweets)ますか?

事前にご指摘いただきありがとうございます。

4

0 に答える 0