undefined method `using_object_ids?

909 views
Skip to first unread message

Marcelo Barbudas

unread,
Sep 25, 2010, 10:52:46 AM9/25/10
to Mongoid
Hi!

Apparently I've hit the same bug as:
http://github.com/mongoid/mongoid/issues/issue/332

In my case, I'm just trying to save a mongo model, nothing special. It
has a number of _id fields which reference ActiveRecord.


NoMethodError (undefined method `using_object_ids?' for #<Class:
0x117f374c>):
bundle/ruby/1.8/gems/activerecord-3.0.0/lib/active_record/base.rb:
1016:in `method_missing'
bundle/ruby/1.8/gems/mongoid-2.0.0.beta.18/lib/mongoid/extensions/
objectid/conversions.rb:41:in `cast!'
bundle/ruby/1.8/gems/mongoid-2.0.0.beta.18/lib/mongoid/field.rb:
51:in `set'
bundle/ruby/1.8/gems/mongoid-2.0.0.beta.18/lib/mongoid/attributes.rb:
164:in `typed_value_for'
bundle/ruby/1.8/gems/mongoid-2.0.0.beta.18/lib/mongoid/attributes.rb:
132:in `write_attribute'

--
M.

Eldon

unread,
Sep 27, 2010, 7:02:01 PM9/27/10
to Mongoid
I encountered the same issue in an application that interacts with
both Mongoid and ActiveRecord (using recent versions of Mongoid).

It seemed to be related to the changes to the Mongoid switching to
using true object_ids instead of strings representations of those
ID's, unfortunately I haven't had time to dig any deeper into it yet
but I hacked past it for now by simply adding a using_object_ids?
method to all ActiveRecord models like this:

ActiveRecord::Base.instance_eval do
def using_object_ids?
false
end
end

-- Eldon

Marcelo Barbudas

unread,
Sep 28, 2010, 11:06:47 AM9/28/10
to mon...@googlegroups.com
Hey Eldon,

> ActiveRecord::Base.instance_eval do
> def using_object_ids?
> false
> end
> end

Great fix, thanks.

--
M.

Nick Hoffman

unread,
Nov 4, 2010, 12:38:55 AM11/4/10
to Mongoid
On Sep 27, 7:02 pm, Eldon <alameda.el...@gmail.com> wrote:
> I encountered the same issue in an application that interacts with
> both Mongoid andActiveRecord(using recent versions of Mongoid).
>
> It seemed to be related to the changes to the Mongoid switching to
> using true object_ids instead of strings representations of those
> ID's, unfortunately I haven't had time to dig any deeper into it yet
> but I hacked past it for now by simply  adding a using_object_ids?
> method to allActiveRecordmodels like this:
>
> ActiveRecord::Base.instance_eval do
>   def using_object_ids?
>     false
>   end
> end
>
> -- Eldon

Hi Eldon. I tried your suggestion, but was unable to get it to work:
https://gist.github.com/662139

Would you mind posting an example of how you setup a relationship
between a Mongoid::Document class and an ActiveRecord::Base class,
please?

Thanks!
Nick

Nick Hoffman

unread,
Nov 4, 2010, 8:08:03 PM11/4/10
to Mongoid
On Nov 4, 12:38 am, Nick Hoffman <n...@deadorange.com> wrote:
> Hi Eldon. I tried your suggestion, but was unable to get it to work:https://gist.github.com/662139
>
> Would you mind posting an example of how you setup a relationship
> between a Mongoid::Document class and an ActiveRecord::Base class,
> please?
>
> Thanks!
> Nick

I got Eldon's suggestion working. I must've fat-fingered something
yesterday. Now, I'm able to do this:
m = MongoidModel.last
ar = ActiveRecordModel.last
m.active_record_model = ar
m.save

When I tried this next:
ar.mongoid_model
this error occured:
undefined method `quoted_table_name' for MongoidModel:Class
Adding this to the MongoidModel class fixed the error above:
self.quoted_table_name
'"mongoid_model"'
end

Unfortunately, this error is occuring now, and I'm stumped:

ruby-1.9.2-p0 > ar.mongoid_model
(Object doesn't support #inspect)
=>

Any idea how to solve that one?

Martin Streicher

unread,
Nov 18, 2010, 1:02:19 AM11/18/10
to Mongoid
What is the state of this 'bug'? Is it possible to have AR reference a
Mongoid model and vice versa? If so, can someone post examples of each
model?

Martin Streicher

unread,
Nov 18, 2010, 8:54:58 AM11/18/10
to Mongoid
I made some progress looking into this. I have an AR model and a
Mongoid model and the latter can reference the former just fine. I
have the same issues as the post above, however, when AR tries to
reference the Mongoid model.

ActiveRecord::Base provides an inspect method to name the class and
enumerate its attributes and types. Much like adding
self.quoted_table_name, you can add a simple inspect method.

The real challenge though seems to be the find parameters that AR
sends to Mongoid to look up the referenced class. AR sends something
like...

table_name.id_field = id

because it is expecting the Mongoid class to conform to belongs_to.
Since the query is a string and not a hash, the Mongoid
Criteria#translate method gets confused. It seems like translate
should try to comprehend the sort of query that AR generates for a
has_one.

More concretely...

class MongoUser
include Mongoid::Document

referenced_in :owner, :class_name => 'User'
references_many :individuals

def self.inspect
'MongoUser'
end

def self.quoted_table_name
'mongo_users'
end
end

class User < ActiveRecord::Base
has_one :mongo_user, :foreign_key => 'owner_id'
end

TypeError: can't convert Symbol into String
from /Users/strike/.rvm/gems/ruby-1.8.7-p302@remix3/gems/
mongoid-2.0.0.beta.20/lib/mongoid/criteria.rb:195:in `include?'
from /Users/strike/.rvm/gems/ruby-1.8.7-p302@remix3/gems/
mongoid-2.0.0.beta.20/lib/mongoid/criteria.rb:195:in `translate'
from /Users/strike/.rvm/gems/ruby-1.8.7-p302@remix3/gems/
mongoid-2.0.0.beta.20/lib/mongoid/finders.rb:68:in `find'
from /Users/strike/.rvm/gems/ruby-1.8.7-p302@remix3/gems/
activerecord-3.0.3/lib/active_record/associations/
has_one_association.rb:88:in `find_target'

Martin Streicher

unread,
Nov 18, 2010, 9:23:14 AM11/18/10
to Mongoid
This may be a little ugly, but it's a first pass and it does satisfy
the AR belongs_to lookup. This replaces criteria.rb#translate. I guess
the next step is to sanity check and see if there is some other
solution and/or generalize so AR has_many works as well.


def self.translate(*args)
klass = args[0]
params = args[1] || {}
unless params.is_a?(Hash)
return klass.criteria.id_criteria(params)
end
conditions = params.delete(:conditions) || {}

if conditions.is_a?(String)
if (matches = conditions.match(/#{klass.to_s.tableize}\.
([[:alpha:]]+_id)/))
conditions = { matches[1].to_sym => conditions.split('=')
[1].to_i }
end
elsif conditions.include?(:id)
conditions[:_id] = conditions[:id]
conditions.delete(:id)
end

return klass.criteria.where(conditions).extras(params)
end

Martin Streicher

unread,
Nov 18, 2010, 10:02:50 AM11/18/10
to Mongoid
Ah, and the tableize may be something to replace to keep this code non-
AR-specific.


On Nov 18, 9:23 am, Martin Streicher <martin.streic...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages