Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails scope - how to join 3 tables

I have 3 tables:

schools{id, name, desc, adress}
reviews{id, content, rating, school_id, user_id} # user_id & schoold_id is foregin keys
users{id, name, city}

How do I write a rails scope the joins all 3 tables and grab the column the schools.name , reviews.content and reviews.rating, the users.name

I am using rails 3.2


1 Answers

scope :mashup, joins(:school, :user).select("content, rating, schools.name as sname, users.name as uname")

You can learn more here

like image 179
dmoss18 Avatar answered Dec 17 '25 13:12

dmoss18



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!