#1057 ✓not-applicable
mpd

has n associations make it impossible to override methods on the other side of the association

Reported by mpd | September 22nd, 2009 @ 10:30 PM

This is somewhat hard to explain, but if you have:

Class Foo
  has n, :bars
end

Class Bar
  property :foo_type, String
  property :foo_id, String

  def foo
    # find and return the correct foo
  end

  def foo=(other)
    # set foo type and foo id
  end
end

:foo and :foo= within bar are never called if you try to call them within the object. The has n makes it impossible. Instead you see the exceptions:

undefined method get' for nil:NilClass (when calling 'foo' on an instance of Bar)

and

undefined method set' for nil:NilClass (when calling 'foo=' on an instance of Bar)

I have created a toy project at git://github.com/xxx/dm10-model-soup.git, it uses merb, but I don't believe merb is the issue here, and only did it because it was easy to crank this example out with.

edited for code tags

Comments and changes to this ticket

  • mpd
  • mpd

    mpd September 25th, 2009 @ 11:37 PM

    All of my remaining red specs are due to this now. It's the only issue I've run into that can't be worked around in some way without some serious rewrites.

  • mpd

    mpd September 30th, 2009 @ 05:26 PM

    this can be worked around by adding the belongs_to, which then allows the methods to be overridden as expected.

    it does lead to a useless field in the database, however, so i think this is still an issue.

  • mpd

    mpd September 30th, 2009 @ 05:39 PM

    above workaround does not fully work.

    it does allow for overriding of the setter, but getter overrides will still be ignored.

  • Jonathan Stott (namelessjon)

    Jonathan Stott (namelessjon) October 9th, 2009 @ 09:22 AM

    • State changed from “new” to “hold”

    Could you rewrite your example as a standalone script? They're much easier to work with for developers who want to see the situation you're dealing with. Something like:

    require 'rubygems'
    require 'dm-core'
    
    DataMapper.setup(:default, 'sqlite3::memory:')
    
    # models go here
    
    DataMapper.auto_migrate!
    
    # code which shows problem goes here.
    

    Or try gem install namelessjon-dm-gen and then dm-gen one_file filename.rb

  • mpd
  • Jared Morgan

    Jared Morgan November 29th, 2009 @ 03:10 PM

    • Tag cleared.

    Another workaround is reopen the class (Bar class in the example) and add the methods after all the models have loaded. I'm not sure what exactly that means in every case, but in mpd's gist, add the methods after DataMapper.auto_migrate! ( See http://gist.github.com/245058 ). In a Merb app, adding the methods in init.rb's "Merb::BootLoader.after_app_loads" section seems to work.

  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) March 24th, 2011 @ 07:08 AM

    • State changed from “hold” to “not-applicable”
    • Milestone order changed from “0” to “0”

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 »

Referenced by

Pages