2013-04-30 1 views
0

У меня есть эта схема дбСписок через has_many

class User 
    has_many :accounts 
end 

class Account 
    belongs_to :user 
    belongs_to :biller 
end 

class Biller 
    has_many :accounts 
end 

Как получить список Billers от пользователя?

billers = user.? 

ответ

1

Добавить has_many ассоциации с основательным вариантом:

class User 
    has_many :accounts 
    has_many :billers, through: :accounts 
end 

class Account 
    belongs_to :user 
    belongs_to :biller 
end 

class Biller 
    has_many :accounts 
end 

И использовать его как следующие:

billers = user.billers 

Более подробной информации см в Active Record guide.

+0

Обязательно добавьте ': user_id' и': biller_id' в таблицу учетных записей в своей базе данных. – mmichael

+0

Если бы их еще не было, его существующие классы не сработали. – sevenseacat

Смежные вопросы