how to perform validation prior to creating a new association between records?

12 views
Skip to first unread message

coriordan

unread,
Jan 3, 2013, 10:13:59 AM1/3/13
to rubyonra...@googlegroups.com
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.

Dheeraj Kumar

unread,
Jan 3, 2013, 1:57:45 PM1/3/13
to rubyonra...@googlegroups.com
The validation would go in the Instructorship model.


-- 
Dheeraj Kumar

--
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...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
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.
 
 

coriordan

unread,
Jan 4, 2013, 4:20:26 AM1/4/13
to rubyonra...@googlegroups.com
Thanks Dheeraj :)
Reply all
Reply to author
Forward
0 new messages