#166 ✓resolved
Paul Barry

habtm doesn't associated objects when saving parent

Reported by Paul Barry | February 23rd, 2008 @ 10:57 AM

If you run this:

require 'rubygems'
require 'data_mapper'

DataMapper::Database.setup({
  :adapter => "sqlite3",
  :database => "blog.db",
  :log_stream => STDOUT,
  :log_level => 0  
})

class Post < DataMapper::Base
  property :title, :string
  has_many :comments
  has_and_belongs_to_many :tags
end

class Comment < DataMapper::Base  
  belongs_to :post
  property :body, :text
end

class Tag < DataMapper::Base
  property :name
  has_and_belongs_to_many :posts
end

DataMapper::Base.auto_migrate!

p = Post.new(:title => "foo")
p.comments << Comment.new(:body => "blah, blah, blah")
p.tags << Tag.new(:name => "bar")
p.save

You will see that the comment gets created and associated to the post, but the tag doesn't get created, but it does try to associate it:

INSERT INTO "posts_tags" ("post_id", "tag_id") VALUES (1, NULL)

Comments and changes to this ticket

  • Paul Barry

    Paul Barry February 24th, 2008 @ 09:36 AM

    Here's a spec. I've been looking into the problem and I haven't come up with a solution yet. The problem is that in has_and_belongs_to_many.save_without_validation, it calls dirty?. This adds the associated object to cleared. Then, when it tries to save the associated object, it passed cleared to data_objects_adapater.save_without_validation, which checks to see if cleared includes the object, which it does, so it returns without saving the object.

  • Paul Barry
  • Dan Kubb (dkubb)
  • Sam Smoot

    Sam Smoot May 2nd, 2008 @ 04:40 AM

    • Assigned user set to “Scott Bauer”

    Scott, PDI. We'll stay on DM3 for the project at work for the next week or so, so it looks like Keith's discovered some issues that makes me think our biggest wins are going to be association fixes for 0.3.0.

    So if some fixes tomorrow give us some breathing room to make a solid 0.9.0 release sometime next week, that seems like the better route for now...

  • Scott Bauer

    Scott Bauer May 8th, 2008 @ 11:03 PM

    • State changed from “new” to “open”

    Sam, Keith hacked in a solution for our specific DM 0.3.0 problems, but I don't think any big changes were made as far as the core of DM associations go. Since its looking like 0.9.0 might be out next week, I say we hold out for that before we spend time chasing down the 0.3.0 issue.

  • Sam Smoot

    Sam Smoot May 8th, 2008 @ 11:19 PM

    Agreed.

    Except... I somehow missed that there's a patch attached to this. :p

    No harm just applying/fixing it. Can you take care of that?

  • Sam Smoot

    Sam Smoot May 20th, 2008 @ 08:33 PM

    • Assigned user changed from “Scott Bauer” to “Adam French”

    Ok, we didn't get to this. Seems kinda silly now with 0.9.0 out next week, but might as well try to apply this for people with running applications on 0.3.x.

    Adam, can do?

  • Adam French
  • Adam French

    Adam French May 20th, 2008 @ 09:34 PM

    • State changed from “open” to “resolved”

    patch appliedin in r757, thanks guys!

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 »

People watching this ticket

Pages