简体   繁体   中英

Getting associated records in Rails

This one should be easy.

I have @restaurants which contains a selected number of records from the Restaurant model. Each Restaurant has one or more locations. I would like to store in an instance variable all of the locations corresponding with all of the restaurants in @restaurants.

This doesn't work:

@locations = @restaurants.locations

What should I do?

@locations = Location.where(restaurant_id: @restaurants).all

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