I am attempting to run some raw SQL in a resource object. in this case it is
much more efficient to run these simple queries then to grab all the rows
with a column value greater then 5 and iterate aver them all and save the
new values.

I need to run two queries as follows:

"UPDATE categories SET rgt=rgt+2 WHERE rgt>#{parent.lft}"
"UPDATE categories SET lft=lft+2 WHERE lft>#{parent.lft}"


The Code that I want to run this in:

parent = Category.get(self.parent_id)
next_in_tree = Category.first(:lft => parent.lft+1)
repository.adapter.query("UPDATE categories SET rgt=rgt+2 WHERE
rgt>#{parent.lft}")
repository.adapter.query("UPDATE categories SET lft=lft+2 WHERE
lft>#{parent.lft}")
next_in_tree.reload
self.lft = parent.lft + 1
self.rgt = next_in_tree.lft - 1
self.save


The update SQL does not return any data and before ti fully runs is causing
an issue with Adapter object throwing this error:

NoMethodError: undefined method `fields' for nil:NilClass

from
/usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.11/lib/dm-core/adapters/data_objects_adapter.rb:100:in
`query'
from
/usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.11/lib/dm-core/adapters/data_objects_adapter.rb:187:in
`with_reader'
from
/usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.11/lib/dm-core/adapters/data_objects_adapter.rb:173:in
`with_connection'
from
/usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.11/lib/dm-core/adapters/data_objects_adapter.rb:183:in
`with_reader'
from
/usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.11/lib/dm-core/adapters/data_objects_adapter.rb:97:in
`query'
from /var/u/apps/race/releases/20090915195116/app/models/category.rb:38:in
`make_space_in_tree'
from
/usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:253:in
`execute_after_create_nan_hook_stack'
from
/usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:300:in
`create'
from
/usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:297:in
`catch'
from
/usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:297:in
`create'
from
/usr/local/lib/ruby/gems/1.8/gems/dm-core-0.9.11/lib/dm-core/resource.rb:295:in
`hookable__save_nan_before_advised'
from
/usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:299:in
`save'
from
/usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:297:in
`catch'
from
/usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.11/lib/extlib/hook.rb:297:in
`save'
from (irb):1
from :0


As you can see I am using DM 0.9.11. Somewhere in
`repository.adapter.query("UPDATE
categories SET rgt=rgt+2 WHERE rgt>#{parent.lft}")` I am running into an
issue. The documentation while in the process of being worked on is lacking
here so I am at a quandary as to how to resolve this.


----------------------
Ian Sheridan
http://iansheridan.dyndns.org
----------------------

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to