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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On May 10, 2010, at 6:41 PM, David Zhu wrote:> 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? thanksI 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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!! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Zhu <dzwestwindsor45-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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!! > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Thanks & Regards, Dhruva Sagar. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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?) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 guysYou''re welcome!> > (Oh, am i posting right?)Yup. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 12 May 2010 21:05, Jesse <dracorna-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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_membershipsCan you provide the full associations for each class, ie Portal, Program, User and ProgramMembership? I don''t clearly understand what relates to what. Colin> > 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 > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, May 12, 2010 at 4:47 PM, Jesse <dracorna-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sure thing: > > class Portal < ActiveRecord::Base > has_many :users, :through => :programs > end > > class Program < ActiveRecord::Base > has_many :users, :through => :program_memberships > end > > class User < ActiveRecord::Base > has_many :program_memberships, :dependent => :destroy > has_many :programs, :through => :program_memberships > has_many :portals, :through => :programs > endI''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 -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 5/12/10 6:08 PM, Rick DeNatale wrote:> On Wed, May 12, 2010 at 4:47 PM, Jesse<dracorna-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Sure thing: >> >> class Portal< ActiveRecord::Base >> has_many :users, :through => :programs >> end >> >> class Program< ActiveRecord::Base >> has_many :users, :through => :program_memberships >> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.