Similar Setup on Stackoverflow
Hi there,
I have a little problem with my nested form setup...
Model "TimeShifting"
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
Join Model "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
Model "ArticlePosition"
class ArticlePosition < ActiveRecord::Base
has_many :article_position_time_shifting_assignments
has_many :time_shiftings, :through => :article_position_time_shifting_assignments
end
The important point in here is that I have some extra attributes in the 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 and updated_by are getting filled out automaticly, order_number and confirmed_date through the form.
Well... I have absolutly no problem to create a new ArticlePosition with
<%= f.simple_fields_for :article_position_time_shifting_assignments do |builder| %>
and then
<%= f.simple_fields_for :article_position do |builder| %>
My problem is that it's always creating a new article_position_time_shifting_assignment record. Even when it's not creating a new 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: "">
Mhhhh... I don't want that :P
accepts_nested_attributes_for :article_position_time_shifting_assignments, :reject_if => proc { |obj| obj['article_position_id'].blank? }
This won't work because there is no article_position_id until the corresponding article_position has been saved :-/
Any ideas to solve this problem?
Cheers,
Michael