#725 ✓resolved
dmilith

BUG with many to many association

Reported by dmilith | December 21st, 2008 @ 09:08 PM


class Domain
  include DataMapper::Resource

  property  :id,        Serial,  :index => true
  property  :name,      String,     :nullable => false,  :default => "drakor.eu"
  property  :owner_id,  Integer,  :index => true, :default => 1
  property :created_at, DateTime
  property :created_on, Date
  property :updated_at, DateTime
  property :updated_on, Date

  has n, :usage_permissions
  has n, :users, :through => :usage_permissions, :mutable => true # workaround XXX http://datamapper.lighthouseapp.com/projects/20609/tickets/485-has-n-through-does-not-allow-saving-of-model
  has n, :subdomains
  belongs_to :owner, :class_name => "User"
end


class UsagePermission
  include DataMapper::Resource

    property :id,                              Serial,  :index => true
    property :available_subdomains_count,   Integer
    property :domain_id,                      Integer,  :index => true
    property :user_id,                        Integer,  :index => true
    property :created_at, DateTime
    property :created_on, Date
    property :updated_at, DateTime
    property :updated_on, Date

    belongs_to :user
    belongs_to :domain
end


class User
  include DataMapper::Resource

  has n, :usage_permissions
  has n, :domains, :through => :usage_permissions, :mutable => true # workaround XXX http://datamapper.lighthouseapp.com/projects/20609/tickets/485-has-n-through-does-not-allow-saving-of-model
  has n, :subdomains
  has n, :ports
  has n, :memory_usage_records

# ...
end


@user = User.first # ok

@domain = Domain.new # ok

@domain.owner = @user # ok but why the hell it's creating user_id and owner_id ? I don't have such column.. anyway..

@domain.valid? # true

@domain.users << @user # ok!

@domain.valid? # true

@domain.save # BIG FAT BUG:

NoMethodError: undefined method attach_parent' for #<DataMapper::Associations::RelationshipChain:0x7fc73de0ddd0> <br/>

    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/associations/one_to_many.rb:302:in `save_resource'
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/associations/relationship.rb:172:in `with_repository'                                                                                                              
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/support/kernel.rb:6:in `repository'          
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core.rb:181:in `repository'                       
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/repository.rb:44:in `scope'                  
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core.rb:181:in `repository'                       
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/support/kernel.rb:6:in `repository'          
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/associations/relationship.rb:172:in `with_repository'                                                                                                              
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/associations/one_to_many.rb:298:in `save_resource'                                                                                                                 
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/associations/one_to_many.rb:210:in `save'    
    from /usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.9/lib/extlib/lazy_array.rb:391:in `each'                    
    from /usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.9/lib/extlib/lazy_array.rb:391:in `send'                    
    from /usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.9/lib/extlib/lazy_array.rb:391:in `method_missing'          
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/collection.rb:638:in `method_missing'        
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/associations/one_to_many.rb:309:in `send'    
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/associations/one_to_many.rb:309:in `method_missing'                                                                                                                
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/associations/one_to_many.rb:210:in `save'    
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/resource.rb:286:in `hookable__save_nan_before_advised'                                                                                                             
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/resource.rb:286:in `each'                    
    from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.8/lib/dm-core/resource.rb:286:in `hookable__save_nan_before_advised'                                                                                                             
    from /usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.9/lib/extlib/hook.rb:294:in `save'                          
    from /usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.9/lib/extlib/hook.rb:292:in `catch'                         
    from /usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.9/lib/extlib/hook.rb:292:in `save'                          
    from (irb):10

Comments and changes to this ticket

  • dmilith

    dmilith December 21st, 2008 @ 09:31 PM

    z = Domain.new :owner => User.first ~ SELECT "id", "login", "pass", "first_name", "last_name", "email", "telephone", "address", "admin", "active", "activation_token", "salt", "rack_access", "vhost_access", "mail_access", "database_access", "cookie_hash", "created_at", "created_on", "updated_at", "updated_on" FROM "users" ORDER BY "id" LIMIT 1 => #z.users << User.first ~ SELECT "id", "login", "pass", "first_name", "last_name", "email", "telephone", "address", "admin", "active", "activation_token", "salt", "rack_access", "vhost_access", "mail_access", "database_access", "cookie_hash", "created_at", "created_on", "updated_at", "updated_on" FROM "users" ORDER BY "id" LIMIT 1 ~ SELECT "users"."id", "users"."login", "users"."pass", "users"."first_name", "users"."last_name", "users"."email", "users"."telephone", "users"."address", "users"."admin", "users"."active", "users"."activation_token", "users"."salt","users"."rack_access", "users"."vhost_access", "users"."mail_access", "users"."database_access", "users"."cookie_hash", "users"."created_at", "users"."created_on", "users"."updated_at", "users"."updated_on" FROM "users" INNER JOIN "usage_permissions" ON ("users"."id" = "usage_permissions"."user_id") WHERE ("usage_permissions"."domain_id" IS NULL) GROUP BY "users"."id", "users"."login", "users"."pass", "users"."first_name", "users"."last_name", "users"."email", "users"."telephone", "users"."address", "users"."admin", "users"."active", "users"."activation_token", "users"."salt", "users"."rack_access", "users"."vhost_access", "users"."mail_access", "users"."database_access", "users"."cookie_hash", "users"."created_at", "users"."created_on", "users"."updated_at", "users"."updated_on" ORDER BY "users"."id" => [#dmilith@drakor.eu" telephone="666" address="Szatan kom" admin=true active=true activation_token=nil salt="700815041173800.779605544316286" rack_access=true vhost_access=true mail_access=true database_access=true cookie_hash=nil created_at=#<DateTime: 212096676163/86400,1/24,2299161> created_on=#<Date: 4909645/2,0,2299161> updated_at=#<DateTime: 212096676163/86400,1/24,2299161> updated_on=#<Date: 4909645/2,0,2299161>>]

    to show that it SHOULD work. and in irb it works.. until save...

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) January 8th, 2009 @ 05:17 AM

    • State changed from “unconfirmed” to “accepted”
    • Assigned user set to “Dan Kubb (dkubb)”
  • A. Jelveh

    A. Jelveh February 10th, 2009 @ 02:02 PM

    +1 same problem here ... setup is basically the same...

  • Justin Smestad

    Justin Smestad February 23rd, 2009 @ 04:37 PM

    As far as I can tell this is still occuring in dm-next. One thing to note is if @user = nil when the << occurs, the save will go through fine. So the case is most likely isolated to the attachment mechanism.

  • Tony Pitale

    Tony Pitale May 3rd, 2009 @ 05:19 PM

    +1 same problem here, same type of association with :through

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 15th, 2009 @ 06:49 PM

    • State changed from “accepted” to “resolved”

    This appears to be working on the dm-core/next branch. I am marking as resolved.

    Please confirm if the problem is fixed for you, and if not I will re-open this ticket.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 15th, 2009 @ 06:51 PM

    Attached is the script I used to confirm the issue is fixed.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Attachments

Pages