I have a situation where I need to get Courses for a particular User. Courses are associated with Divisions that in turn are related to the user by a has_many, through relationship. Ideally, I''d like to stay within the framework and do something like this:> @courses = @user.divisions.courses >Or if I''m looking for a particular course I could do this:> @course = @user.divisions.courses.find(Params[:id]) >Is this completely outside of the framework? Here is the model setup I have:> class User < ActiveRecord::Base > has_many :schedulers > has_many :divisions, :through => :schedulers > end > > class Scheduler < ActiveRecord::Base > belongs_to :division > belongs_to :user > end > > > class Division < ActiveRecord::Base > has_many :schedulers > has_many :users, :through => :schedulers > > has_many :courses > end > > class Course < ActiveRecord::Base > belongs_to :division > belongs_to :department > end >I managed to get a list of all Courses by adding a function in the User model, but this isn''t ideal for a few reasons, the most important being if I only need to find one Course, I''m forced to retrieve all of the courses and then perform an @courses.select{yada yada}. There must be a Better Way(tm)! Any ideas? class User < ActiveRecord::Base> has_many :schedulers > has_many :divisions, :through => :schedulers > > def get_courses() > courses = Array.new > > self.divisions.each do |division| > courses.push(division.courses) > end > > return courses.flatten > end > > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m assuming you know the ID of the course you want to select. Why don''t you just do Course.find(params[:id]) instead of: @course = @user.divisions.courses.find(Params[:id]) On Dec 7, 2007 1:58 AM, Joe Cairns <joe.cairns-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a situation where I need to get Courses for a particular User. > Courses are associated with Divisions that in turn are related to the user > by a has_many, through relationship. Ideally, I''d like to stay within the > framework and do something like this: > > > @courses = @user.divisions.courses > > > > Or if I''m looking for a particular course I could do this: > > > @course = @user.divisions.courses.find(Params[:id]) > > > > Is this completely outside of the framework? > > Here is the model setup I have: > > > class User < ActiveRecord::Base > > has_many :schedulers > > has_many :divisions, :through => :schedulers > > end > > > > class Scheduler < ActiveRecord::Base > > belongs_to :division > > belongs_to :user > > end > > > > > > class Division < ActiveRecord::Base > > has_many :schedulers > > has_many :users, :through => :schedulers > > > > has_many :courses > > end > > > > class Course < ActiveRecord::Base > > belongs_to :division > > belongs_to :department > > end > > > > I managed to get a list of all Courses by adding a function in the User > model, but this isn''t ideal for a few reasons, the most important being if I > only need to find one Course, I''m forced to retrieve all of the courses and > then perform an @ courses.select{yada yada}. There must be a Better > Way(tm)! Any ideas? > > class User < ActiveRecord::Base > > has_many :schedulers > > has_many :divisions, :through => :schedulers > > > > def get_courses() > > courses = Array.new > > > > self.divisions.each do |division| > > courses.push(division.courses) > > end > > > > return courses.flatten > > end > > > > end > > > > > > > > >-- Ryan Bigg --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hey, thanks for the reply. It''s a validation check to make sure that the url isn''t spoofed by the user. We don''t want one user editing a course that isn''t associated to them: www.example.com/course/edit/6 Changed to www.example.com/course/edit/16 On Dec 7, 2007 12:06 AM, Ryan Bigg <radarlistener-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m assuming you know the ID of the course you want to select. > > Why don''t you just do > > Course.find(params[:id]) > > instead of: > > @course = @user.divisions.courses.find(Params[:id]) > > On Dec 7, 2007 1:58 AM, Joe Cairns <joe.cairns-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have a situation where I need to get Courses for a particular User. > > Courses are associated with Divisions that in turn are related to the user > > by a has_many, through relationship. Ideally, I''d like to stay within the > > framework and do something like this: > > > > > @courses = @user.divisions.courses > > > > > > > Or if I''m looking for a particular course I could do this: > > > > > @course = @user.divisions.courses.find(Params[:id]) > > > > > > > Is this completely outside of the framework? > > > > Here is the model setup I have: > > > > > class User < ActiveRecord::Base > > > has_many :schedulers > > > has_many :divisions, :through => :schedulers > > > end > > > > > > class Scheduler < ActiveRecord::Base > > > belongs_to :division > > > belongs_to :user > > > end > > > > > > > > > class Division < ActiveRecord::Base > > > has_many :schedulers > > > has_many :users, :through => :schedulers > > > > > > has_many :courses > > > end > > > > > > class Course < ActiveRecord::Base > > > belongs_to :division > > > belongs_to :department > > > end > > > > > > > I managed to get a list of all Courses by adding a function in the User > > model, but this isn''t ideal for a few reasons, the most important being if I > > only need to find one Course, I''m forced to retrieve all of the courses and > > then perform an @ courses.select{yada yada}. There must be a Better > > Way(tm)! Any ideas? > > > > class User < ActiveRecord::Base > > > has_many :schedulers > > > has_many :divisions, :through => :schedulers > > > > > > def get_courses() > > > courses = Array.new > > > > > > self.divisions.each do |division| > > > courses.push(division.courses) > > > end > > > > > > return courses.flatten > > > end > > > > > > end > > > > > > > > > > > > > > > > > > > > -- > Ryan Bigg > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Joe Cairns wrote:> Hey, thanks for the reply. > > It''s a validation check to make sure that the url isn''t spoofed by the > user. We don''t want one user editing a course that isn''t associated to > them: > www.example.com/course/edit/6 > > Changed to > www.example.com/course/edit/16fine, take all my fun away. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---