你好呀,
我的嵌套表单设置有一点问题......
模型“时移”
class TimeShifting < ActiveRecord::Base
has_many :article_position_time_shifting_assignments, :dependent => :destroy
has_many :article_positions, :through => :article_position_time_shifting_assignments
accepts_nested_attributes_for :article_position_time_shifting_assignments
end
加入模型“ArticlePositionTimeShiftingAssignment”
class ArticlePositionTimeShiftingAssignment < ActiveRecord::Base
belongs_to :article_position
belongs_to :time_shifting
accepts_nested_attributes_for :article_position, :reject_if => proc { |obj| obj['baan_id'].blank? }
end
模型“文章位置”
class ArticlePosition < ActiveRecord::Base
has_many :article_position_time_shifting_assignments
has_many :time_shiftings, :through => :article_position_time_shifting_assignments
end
这里的重点是我在 Join Model ArticlePositionTimeShiftingAssignment 中有一些额外的属性...
create_table "article_position_time_shifting_assignments", :force => true do |t|
t.integer "article_position_id"
t.integer "time_shifting_id"
t.integer "created_by"
t.integer "updated_by"
t.datetime "created_at"
t.datetime "updated_at"
t.string "order_number"
t.date "confirmed_date"
t.string "purchase_positions_collection"
end
created_by 和 updated_by 正在自动填写,order_number 和 confirm_date 通过表格。
嗯......我绝对没有问题创建一个新的文章位置
<%= f.simple_fields_for :article_position_time_shifting_assignments do |builder| %>
接着
<%= f.simple_fields_for :article_position do |builder| %>
我的问题是它总是创建一个新的 article_position_time_shifting_assignment 记录。即使它没有创建新的 ArticlePosition。
#<ArticlePositionTimeShiftingAssignment id: 10, article_position_id: nil, time_shifting_id: 10, created_by: 1, updated_by: 1, created_at: "2012-05-23 14:57:27", updated_at: "2012-05-23 14:57:27", order_number: "", confirmed_date: nil, purchase_positions_collection: "">
嗯......我不想要那个:P
accepts_nested_attributes_for :article_position_time_shifting_assignments, :reject_if => proc { |obj| obj['article_position_id'].blank? }
这将不起作用,因为在保存相应的 article_position 之前没有 article_position_id :-/
有什么想法可以解决这个问题吗?
干杯,
迈克尔