I have two models, A and B, related to one another by a many to many association and I want to validate an object of model B as it''s being added to the association based on both its own attributes and attributes of the particular object of model A that it''s being associated with. I''ve realized that I can''t do this if the association is represented by a habtm relationship since (as far as I can tell - please tell me if I''m wrong) there''s no way to refer to the A object in my validation method for B. Has many :through looks like a better approach here, since I can define the validation method on the join model itself. However this raises a question. I can''t create a has many :through association unless the objects on both ends of the association are already saved (right?). But I don''t want B to be saved if the association is going to be invalid. Does this imply that the only way to handle this is to use a transaction to wrap the saving of B and the creation of the association? Or is there a smarter way to do this? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
MarkMT wrote:> I have two models, A and B, related to one another by a many to many > association and I want to validate an object of model B as it''s being > added to the association based on both its own attributes and > attributes of the particular object of model A that it''s being > associated with. > > I''ve realized that I can''t do this if the association is represented > by a habtm relationship since (as far as I can tell - please tell me > if I''m wrong) there''s no way to refer to the A object in my validation > method for B.You could arrange to get access to the A object using something like: class A < ActiveRecord::Base has_and_belongs_to_many bs, :before_add => :set_b_current_a def set_b_current_a(b) b.current_a = self end end class B < ActiveRecord::Base has_and_belongs_to_many as attr_accessor :current_a end -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks, that''s pretty interesting. Mark. On Aug 25, 11:06 pm, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> MarkMT wrote: > > I have two models, A and B, related to one another by a many to many > > association and I want to validate an object of model B as it''s being > > added to the association based on both its own attributes and > > attributes of the particular object of model A that it''s being > > associated with. > > > I''ve realized that I can''t do this if the association is represented > > by a habtm relationship since (as far as I can tell - please tell me > > if I''m wrong) there''s no way to refer to the A object in my validation > > method for B. > > You could arrange to get access to the A object using something like: > > class A < ActiveRecord::Base > has_and_belongs_to_many bs, :before_add => :set_b_current_a > def set_b_current_a(b) b.current_a = self end > end > > class B < ActiveRecord::Base > has_and_belongs_to_many as > attr_accessor :current_a > end > > -- > We develop, watch us RoR, in numbers too big to ignore.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---