PeaceB
PeaceB

Reputation: 1

Rails:scope from parents of parents of parents

I've got a relation below

    School
    has_many :students
    has_many :accounts, :through => :students

    Student
    belongs_to :school
    has_one :account

    Account
    belongs_to :student

This is question, I've got couple of schools
and I want to get accounts belongs specific school with pagination
How can I load this?

    @accounts = Accounts.where(...).page(params[:page]).per(10)


I missed my question
I've got a relation below

    School
    has_many :classrooms

    Classroom
    belongs_to :school
    has_many :students
    has_many :accounts, :through => :students

    Student
    belongs_to :classroom
    has_one :account

    Account
    belongs_to :student

and question is same :)

Upvotes: 0

Views: 42

Answers (2)

oreoluwa
oreoluwa

Reputation: 5633

I think you should be able to do:

school = School.first
@accounts = school.accounts.page(params[:page]).per(10)

UPDATE Based on the updated question, you can still do and achieve the same result by using the query above:

School
  has_many :classrooms
  has_many :accounts, through: :classrooms

Upvotes: 1

Vishal
Vishal

Reputation: 7361

you should find the school by id whatever school you want

@school = School.find(params[:id])

and you can get accounts associated with that particular school by this statement

@accounts = @school.accounts.page(params[:page]).per(10)

Upvotes: 0

Related Questions