1

Ruby on Rails (バージョン 3.2.2) と mysql2 (バージョン 0.3.11 - MySQL バージョン 5.2.38) を使用しています。「関連テーブルを介して、一連のユーザーが「共通」に持つ関連オブジェクト/レコードを取得する方法」について議論しているときに*私は、「関連オブジェクト/レコード」を見つけなければならない「特定の」状況にいることに気づきました**パフォーマンス上の理由から、関連付けデータベース テーブルに対して単一の SQL クエリを実行することによって。特に、前述の「関連付けられたオブジェクト/レコード」のみを取得するために、 (オブジェクトへの「ポインティング」を表す)および( 「ポインティング」を表す) のみを取得するために作成したいと思います。user_idforeign keyUserarticle_idforeign keyArticle列の値は同じです(つまり、article_iduser_idが「共通」です)。

私の場合、「関連付けられたオブジェクト/レコード」を取得するにはどうすればよいですか?


*具体的には、@Andrew Marshallが回答を投稿した後。

**:これらの「関連オブジェクト/レコード」は、リンクされた質問で説明されているhas_many :throughRuby on Railsに関連しています。ActiveRecord::Associations

4

1 に答える 1

0

これは、必要なものを引き出すための生のSQLの方法だと思います:

select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = 1) and user_id = 2;

Rails でユーザー ID を置き換えることができる場所:

ArticlesUser.find_by_sql(["select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = ?) and user_id = ?", @user1.id, @user2.id])

または複数のIDの場合::

ArticlesUser.find_by_sql(["select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = ?) and user_id IN (?)", @user1.id, [@user2.id,@user3.id]])

サンプルデータから(他の質問から):

mysql> select * from articles_users;
+----+---------+------------+
| id | user_id | article_id |
+----+---------+------------+
|  1 |       1 |          1 |
|  2 |       1 |          2 |
|  3 |       1 |          3 |
|  4 |       2 |          1 |
|  5 |       2 |          2 |
|  6 |       3 |          1 |
|  7 |       3 |          3 |
|  8 |       4 |          4 |
+----+---------+------------+
8 rows in set (0.00 sec)

次のように値を返します。

mysql> select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = 1) and user_id = 2;
+----+---------+------------+
| id | user_id | article_id |
+----+---------+------------+
|  4 |       2 |          1 |
|  5 |       2 |          2 |
+----+---------+------------+
2 rows in set (0.00 sec)

mysql> select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = 1) and user_id = 3;
+----+---------+------------+
| id | user_id | article_id |
+----+---------+------------+
|  6 |       3 |          1 |
|  7 |       3 |          3 |
+----+---------+------------+
2 rows in set (0.00 sec)

または、複数のユーザー ID の場合:

mysql> select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = 1) and user_id in (2,3);
+----+---------+------------+
| id | user_id | article_id |
+----+---------+------------+
|  4 |       2 |          1 |
|  5 |       2 |          2 |
|  6 |       3 |          1 |
|  7 |       3 |          3 |
+----+---------+------------+
4 rows in set (0.00 sec)

あなたはSQLの方法を求めました-しかし、これを行うためのレールのような方法はほぼ確実にあります...しかし、これで始めることができ、ここからリファクタリングできます。

于 2012-09-07T06:28:13.313 に答える