こんにちは。
以下のようなSQLをRailsのActiveRecordに書き換えたいのですが、どのように書き換えられれば良いでしょうか?
UNIONやright join、サブクエリなどを使っており、ActiveRecordでどのように表現できるのかわからず。。。
こんな形で書き換えていけば、できるはずといった例だけでもお教えいただけると幸いです‼
よろしくお願いします‼
lang
1 (select 2 INX.id, 3 INX.created_at, 4 INX.updated_at, 5 INX.title, 6 INX.contents_id as txt_array_max, 7 INX.detail_id as img_array_max, 8 IMG.detail_id as contents_num, 9 IMG.image as img, 10 '' as txt 11 from blog_images as IMG 12 right join 13 (select blog_indices.* 14 from 15 blog_indices 16 where id = :id 17 order by 18 updated_at desc 19 limit 5) as INX 20 on INX.id=IMG.contents_id) 21 union 22 (select 23 INX.id, 24 INX.created_at, 25 INX.updated_at, 26 INX.title, 27 INX.contents_id, 28 INX.detail_id, 29 TXT.detail_id, 30 '', 31 TXT.text 32 from blog_textes as TXT 33 right join 34 (select blog_indices.* 35 from 36 blog_indices 37 where id = :id 38 order by 39 updated_at desc 40 limit 5) as INX 41 on INX.id=TXT.contents_id); 42 ",{:id=>id}])
あなたの回答
tips
プレビュー