Ahhh .count!

Okk

So, I have @totalcount = @school.subjects.count

(School has many subjects, subjects has many teachers)

Now, what if i wanted to count up the amount of teachers within that school?

I need something like @totalcount = @school.subjects.teachers.count, but that doesn't work. i dont know the syntax...

so how do i do something like that? thanks

I don't know your application, but if you do this a lot, you could make the argument that perhaps Teacher should belong_to a School? Unless you've got teachers jumping around different schools... it's a bit de-normalized but it will speed up a lot of queries (maybe, just guessing based on your question).

You could also do

class School...    has_many :teachers, :through => :schools end

And then @school.teachers.count.

Or... iterate over @school.subjects and sum them up... using inject. I can never remember the syntax but google for "ruby inject examples" and you'll find what you want. This could result in a lot of queries if there are a lot of subjects though.

-philip

OK,

But right now, i have--

Schools has many subjects, subjects has many teachers

So instead of-- @totalcount = @school.subjects.count (that just cuonts how many subjects are in the school)

how can i count the teachers in that school? maybe something similar to @totalcount = @school.subjects.teachers.count, or something like that

Do you understand? Thanks for your help!!

Hi well there is no School.subjects.teachers in your case.

You should do something like this :

count = 0

@school.subjects.each {|s| count += s.teachers.count}

It is possible that you could use inject above to shorten it a bit, but not sure about that in this case.

David Zhu wrote:

OK,

But right now, i have--

Schools has many subjects, subjects has many teachers

So instead of-- @totalcount = @school.subjects.count (that just cuonts how many subjects are in the school)

how can i count the teachers in that school? maybe something similar to @totalcount = @school.subjects.teachers.count, or something like that

Do you understand? Thanks for your help!!

This seems like a good use case for has_many :through.

Best,

This seems like a good use case for has_many :through.

It sounds like an excellent case for has_many :through!

haha i just did it using that method, it works great. thanks guys

(Oh, am i posting right?)

David Zhu wrote:

This seems like a good use case for has_many :through.

It sounds like an excellent case for has_many :through!

haha i just did it using that method, it works great. thanks guys

You're welcome!

(Oh, am i posting right?)

Yup.

Best,

Hi All, I am trying to do something similar and am running into an issue on it

I have a setup where Portal has many Programs program has many users through program_memberships user has many portals through program_memberships

I am trying to get it so that portal has many users through programs through program_memberships and that user belongs to portal through the same

so I add has_many :users, :through => :programs to the portal model and belongs_to :portals, :through => :programs to the user model

I get an internal server error 500 for this one. if I switch user to has_many :portals I just get a source error.

Is there something that I am missing or that I am thinking about in a wrong way?

Thanks

Jess

Hi All, I am trying to do something similar and am running into an issue on it

I have a setup where Portal has many Programs program has many users through program_memberships user has many portals through program_memberships

Can you provide the full associations for each class, ie Portal, Program, User and ProgramMembership? I don't clearly understand what relates to what.

Colin

Sure thing:

class Portal < ActiveRecord::Base    has_many :programs    has_many :users, :through => :programs end

class Program < ActiveRecord::Base    belongs_to :portal    has_many :program_memberships, :dependent => :destroy    has_many :users, :through => :program_memberships end

class ProgramMembership < ActiveRecord::Base    belongs_to :user    belongs_to :program end

class User < ActiveRecord::Base    has_many :program_memberships, :dependent => :destroy    has_many :programs, :through => :program_memberships    has_many :portals, :through => :programs end

I'm pretty sure that Rails 2.x still doesn't support transitive has_many :through associations

https://rails.lighthouseapp.com/projects/8994/tickets/1152-support-for-nested-has_many-through-associations

There appears to be a plugin, although I've never used it personally.

http://github.com/ianwhite/nested_has_many_through

Oh Sorry, Im on 3.0.0.b3