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

On May 10, 2010, at 6:41 PM, David Z. wrote:

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.

On Tue, May 11, 2010 at 07:38, David Z. [email protected]
wrote:

how can i count the teachers in that school? maybe something similar
[email protected][email protected]
.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

Thanks & Regards,
Dhruva S…

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 Z. 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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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

David Z. 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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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

On 12 May 2010 21:05, Jesse [email protected] wrote:

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

On Wed, May 12, 2010 at 4:47 PM, Jesse [email protected] wrote:

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.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

On 5/12/10 6:08 PM, Rick DeNatale wrote:

end
#1152 Support for nested :through associations - Ruby on Rails - rails

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

GitHub - ianwhite/nested_has_many_through: rails plugin that allows has_many :through to go through other has_many :throughs

Oh Sorry, Im on 3.0.0.b3