0

Имеем три модели. User, Pet, City.

# user.rb   
class User < ActiveRecord::Base
  has_many :pets
  belongs_to :city
end

# pet.rb
class Pet < ActiveRecord::Base
  belongs_to :user
end

# city.rb
class City < ActiveRecord::Base
  has_many :users
end

Как получить список животных Pet, юзеры которых живут в определенном City

1 ответ 1

1

Можно добавить связь has_many :pets, through: :users в модель City. Тогда у объекта класса City появится метод pets, который будет возвращать животных, владельцы которых из этого города.

2
  • спасибо, работает. только through: :users
    – Alex I
    2 июл 2015 в 4:50
  • @AlexIvanov поправил
    – MAXOPKA
    2 июл 2015 в 5:42

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.