开发者

Ruby on Rails - has_many :through with nested_attributes_for

I'm coming across a problem that I can't find much online for via Google, forums, groups, etc. and so I'm going to raise my hand and ask for help from those who are more wise than I :)

I have a rails project setup that was using nested_attributes_for with a one-to-one relationship between two models. It worked quite easily and as expected until I recently had to modify the project to become a has_many :through relationship.

The code looks something like this:

P model:

has_many :f_ps  
has_many :fs, :through => :f_ps

F model:

has_many :f_ps  
has_many :ps, :through => :f_ps

F_P model:

belongs_to :p  
belongs_to :f  
validates_uniqueness_of :f_id, :scope => :p_id, :message => 'each F must be unique for this P'

As long as the code remains like above everything works fine (except of course CRUD actions via the web GUI). However, as soon as I add in the nested attributes portion to the P model like so:

accepts_nested_attributes_fo开发者_如何学编程r :fs,  
  :allow_destroy => true,  
  :reject_if => proc { |a| a.all? { |k,v| v.blank? } }

It causes my tests to fail and the GUI works (minus the validations are not enforced).

Any ideas as to the proper way to go about getting this validation to work happily along with nested_attributes_for or do I just need to create my own CRUD actions in the P controller to handle it like a year or so ago?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜