繁体   English   中英

将SQL转换为Rails 3 ActiveRecord

[英]Converting SQL to Rails 3 ActiveRecord

我正在寻找一种最接近Rails的方式来检索给定专辑中可用歌曲的类别列表

这是SQL中的album_id = 1

-- Using subselects
select * from categories where id in (
  select distinct category_id
  from categorizations
  where song_id in (select song_id from album_songs
                    where album_id = 1 and available = 't')
)
order by name asc;

-- Using joins
select distinct c.* from categories c
  inner join categorizations cz on c.id = cz.category_id
  left join album_songs a on cz.song_id = a.song_id
where a.album_id = 1 and a.available = 't'
order by c.name asc;

我的工作(虽然很天真!)试图将其移植到ActiveRecord

## attempting to do it like subselects (although they're not really
## subselects, it executes them individually -- from what i've read
## ActiveRecord won't do subselects?)
Category.where('id IN (?)',
  Categorization.select('DISTINCT category_id').where('song_id IN (?)',
    Album.find(1).songs.available.map(&:song_id)
  ).map(&:category_id)
).order('name ASC')

## joins - although at this point it's pretty much all sql
## as i couldn't find a way to do the left join in pure AR
## i'm also duplicating my AlbumSongs.available scope -- is
## that scope reusable here? (outside the AlbumSongs model?)
Category.select('DISTINCT categories.*')
        .joins(:categorizations,
               'LEFT OUTER JOIN album_songs ON categorizations.song_id = album_songs.song_id')
        .where('album_songs.album_id = ? and available', 1)

我要使用最后一个,但似乎我也可以只用SQL编写它?

有什么方法可以改善它,使它更像Rails一样?

好吧,如果您发布模型设置,那肯定会有所帮助。 但是假设:
*歌曲has_many:categories,:through =>类别
*一张专辑上没有很多歌曲

为什么不做:

Album.includes({:songs => {:categorizations => :categories}}).find(1).songs.collect {|s| s.category}.flatten.uniq

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM