has_and_belongs_to_many accessors not working

XMLWordPrintableJSON

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

      I have two models: Group and User with hatbm relations

      class User
      ...
         has_and_belongs_to_many :groups, :class_name=>'Group',:inverse_of=>:users
         has_and_belongs_to_many :admin_of, :class_name=>'Group', :inverse_of=>:admins
      end
      
      class Group
      ...
        has_and_belongs_to_many :users
        has_and_belongs_to_many :admins, :class_name=>'User',:inverse_of=>:admin_of
      end
      

      It looks like when I add a model to array, it saves it's ids, but accessors just return nothing.

      running in console:

      irb(main):032:0> g = Group.create(:name=>'test')
      => #<Group _id: 502fcafa53b8a98e1e000004, ..., user_ids: [], admin_ids: []>
      irb(main):033:0> g.users.push User.last
      => [#<User _id: 502fc92353b8a9172e000001,..., group_ids: ["502fcafa53b8a98e1e000004"], admin_of_ids: []>]
      irb(main):034:0> g.save!
      => true
      irb(main):035:0> g
      => #<Group _id: 502fcafa53b8a98e1e000004, ..., user_ids: ["502fc92353b8a9172e000001"], admin_ids: []>
      irb(main):036:0> g.users
      => [#<User _id: 502fc92353b8a9172e000001, ... , group_ids: ["502fcafa53b8a98e1e000004"], admin_of_ids: []>]
      

      so before saving array is showing fine, but after we load it from ODM:

      irb(main):038:0> Group.last
      => #<Group _id: 502fcafa53b8a98e1e000004, _type: nil, name: "test", description: nil, url: nil, user_ids: ["502fc92353b8a9172e000001"], admin_ids: []>
      irb(main):039:0> Group.last.users
      => []
      

      user_ids are present, and accessors are returning empty array

      What am I doing wrong?

      I mentioned that in rspecs only id saving is checked, but proper work of accessors is not. is that intended behaviour?

            Assignee:
            Durran Jordan
            Reporter:
            vogrelord
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: