1

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

Community
  • 1
  • 1
sufu90
  • 144
  • 2
  • 8

1 Answers1

0

Actually it WILL create new record in Mapping table (ArticlePositionTimeShiftingAssignment) and it is correct because it is many-to-many association. So in this case it is not good approach to assign some additional records to Mapping table.

thesis
  • 2,565
  • 5
  • 26
  • 37
  • Mhh. It makes sense what you are saying. I just don't see the right alternative... When a user creates a time_shifting he should be able to add / create some article_positions. Those article_positions could be used later for other time_shiftings. Each of those created / assigned article_positions should have some additional fields (order_number, delivery_date). Mhh...I would need a table with (time_shifting_id, article_pos_id, foobar_id). Foobar_id would link to the additional attributes. Isn't that exactly the same thing? – sufu90 May 23 '12 at 17:24