coriordan
2013-Jan-03 15:13 UTC
how to perform validation prior to creating a new association between records?
Hi, I''m trying to figure out the correct "Rails" way to perform validation prior to creating a new assocation between my models. Here''s the models: class User < ActiveRecord::Base has_and_belongs_to_many :roles has_many :instructorships has_many :instructed_courses, :through => :instructorships, :source => :course class Course < ActiveRecord::Base has_many :instructorships has_many :instructors, :through => :instructorships, :source => :instructor class Instructorship < ActiveRecord::Base belongs_to :instructor, :class_name => ''User'', :foreign_key => ''user_id'' belongs_to :course I''m trying to introduce a validation whereby only user objects with a role of ''instructor'' can be assigned as an instructor on a course. Firstly, where would this validation go? On the association model? And secondly, what is the right way to set this up? thanks for your help, Cathal. -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/bUuvgClzISMJ. For more options, visit https://groups.google.com/groups/opt_out.
Dheeraj Kumar
2013-Jan-03 18:57 UTC
Re: how to perform validation prior to creating a new association between records?
The validation would go in the Instructorship model. You''ll need a custom method, like so: http://edgeguides.rubyonrails.org/active_record_validations.html#custom-methods -- Dheeraj Kumar On Thursday 3 January 2013 at 8:43 PM, coriordan wrote:> Hi, > > I''m trying to figure out the correct "Rails" way to perform validation prior to creating a new assocation between my models. Here''s the models: > > class User < ActiveRecord::Base > > has_and_belongs_to_many :roles > has_many :instructorships > has_many :instructed_courses, :through => :instructorships, :source => :course > > class Course < ActiveRecord::Base > > has_many :instructorships > has_many :instructors, :through => :instructorships, :source => :instructor > > class Instructorship < ActiveRecord::Base > belongs_to :instructor, :class_name => ''User'', :foreign_key => ''user_id'' > belongs_to :course > > I''m trying to introduce a validation whereby only user objects with a role of ''instructor'' can be assigned as an instructor on a course. > > Firstly, where would this validation go? On the association model? And secondly, what is the right way to set this up? > > thanks for your help, > Cathal. > -- > 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 (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/bUuvgClzISMJ. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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 https://groups.google.com/groups/opt_out.
coriordan
2013-Jan-04 09:20 UTC
Re: how to perform validation prior to creating a new association between records?
Thanks Dheeraj :) On Thursday, 3 January 2013 18:57:45 UTC, Dheeraj Kumar wrote:> > The validation would go in the Instructorship model. > > You''ll need a custom method, like so: > http://edgeguides.rubyonrails.org/active_record_validations.html#custom-methods > > -- > Dheeraj Kumar > > On Thursday 3 January 2013 at 8:43 PM, coriordan wrote: > > Hi, > > I''m trying to figure out the correct "Rails" way to perform validation > prior to creating a new assocation between my models. Here''s the models: > > class User < ActiveRecord::Base > > has_and_belongs_to_many :roles > has_many :instructorships > has_many :instructed_courses, :through => :instructorships, :source => > :course > > class Course < ActiveRecord::Base > > has_many :instructorships > has_many :instructors, :through => :instructorships, :source => > :instructor > > class Instructorship < ActiveRecord::Base > belongs_to :instructor, :class_name => ''User'', :foreign_key => ''user_id'' > belongs_to :course > > I''m trying to introduce a validation whereby only user objects with a role > of ''instructor'' can be assigned as an instructor on a course. > > Firstly, where would this validation go? On the association model? And > secondly, what is the right way to set this up? > > thanks for your help, > Cathal. > > -- > 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 rubyonra...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<javascript:> > . > To unsubscribe from this group, send email to > rubyonrails-ta...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/bUuvgClzISMJ. > For more options, visit https://groups.google.com/groups/opt_out. > > > > >-- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/e7dJDa9EIJwJ. For more options, visit https://groups.google.com/groups/opt_out.