Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1240

HABTM relation not created properly from inside a block

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.3.2
    • Affects Version/s: None
    • Component/s: None

      The following code will create a correct HABTM relationship

      User.create(:login => login, :password => 'password', :password_confirmation => 'password') do |u|
      u.company = Company.first
      u.admin = true
      end
      u = User.first
      u.groups << Group.first

      However the following code:

      User.create(:login => login, :password => 'password', :password_confirmation => 'password') do |u|
      u.company = Company.first
      u.admin = true
      u.groups << Group.first
      end

      results in the following

      User.first.groups
      => [#<Group _id: 4e6d540a8625ee28d2000002, name: "test group", created_at: Mon Sep 12 00:36:26 UTC 2011, updated_at: Mon Sep 12 00:36:26 UTC 2011, user_ids: [], _type: nil, company_id: BSON::ObjectId('4e6d540a8625ee28d2000001')>]
      
      Group.first.users
      => [] 
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            rurounijones Jeffrey Jones
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: