简体   繁体   中英

How do I convert this SQL query into Active Record statements?

Here's the query:

select dogs.winning_status from dogs join cats on dogs.cat_id = cat.id where cats.cat_type = 'big' and dogs.user_id = 29 order by dogs.created_at DESC limit 2;

I have no idea how to convert this into an active record query though. I've looked at numerous rails sites (including this one ) I have no idea how to approach this at all though. I've tried a few different attempts, and all have failed.

This should work if you've got everything set up according to Rails's conventions.

Dog.joins(:cats).select(:winning_status).where("cats.cat_type = 'big' AND dogs.user_id = 29").order(:created_at).limit(2)

or this one

Dog.joins(:cats).select(:winning_status).where("cats.cat_type = ?", 'big').where(user_id: 29).order(:created_at).limit(2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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