ruby conditional validation has_and_belong_to_many -


i having 2 models, user , role,

in role model:

has_and_belongs_to_many :users  

in user model,

has_and_belongs_to_many :roles  validates_presence_of :name, :if => "roles.map(&:name).include?(\"manager\")" 

my problem can add "manager" role roles of user, , go through. in console when call

sampleuser.roles << managerrole 

it goes thorough , saves in db. when call

sampleuser.valid? 

i false,

my understanding because roles_users separate table, nothing on user getting updated, user validations not used. idea how t around problem , make user whatever goes db valid?

as say, can't access records through habtm relationship till habtm link saved. can use after_save callback validate associated records , rollback transaction if validations fail.

see http://mattberther.com/2012/09/09/validating-habtm-relationships-with-rails-3x


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -