1

I have two models;

class Foo
  belongs_to :bar
end
class Bar
  has 1, :foo
end

This all works fine, relationship working fine and so on. The requirement arose for us to override the "all" method on Foo, to always put a condition into any query. We did this like so;

class Foo
   def self.all(opts = {})
     super(opts.merge(:hidden => false))
   end
end

And all that works too, but when I run the following command;

Foo.all.bar

It gives me the following error:

"condition :hidden does not map to a property in Bar"

That line worked totally fine before I overrode 'all'. I don't understand why it's applying "hidden" to the 'bar' object rather than the 'foo' object!

Mikey Hogarth
  • 4,672
  • 7
  • 28
  • 44

1 Answers1

0

If anyone else encounters this, here is how I eventually fixed it. The problem was in the "query" object returned by datamapper - it holds a reference to a "model" and if that isn't the model your condition is on, you get the error I was getting. Basically the way around it is by doing a check against the model like so;

class << self
 #Alias out the original all
 alias :unfiltered_all :all

  def all(opts = {})
    query = unfiltered_all(opts)
     if(query.model.ancestors.include? self)
      return query.unfiltered_all(:hidden => false)
    else
      return query.all(:foo => { :hidden => false } )
    end
  end
end
Mikey Hogarth
  • 4,672
  • 7
  • 28
  • 44