Hmmm, that should work, or at least not blow up in the way that it did.
Can you try a different way of adding to the collection to see if it works?
sale_products.each do |p|
clone.sale_products << p.dup
end
Also, could you try this for me?
z = sale_products.map {|p| p.dup }
clone.sale_products = z
I just want to confirm that we're blowing up on the second line, not the first.
It looks like your doing the permissions correctly, but I haven't used
web methods in a while, so you'll want to explicitly test that it
works.
Bryan
On Sun, Jul 1, 2012 at 6:21 AM, Catbus Omnibus <[email protected]> wrote:
> Hi there,
>
> I have posted a question on Stackoverflow and asked for solution for the
> duplicating a record and its related records (has many).
>
> http://stackoverflow.com/questions/11028413/clone-action-in-hobo/11036041
>
> I write a method to duplicate the records, but in the console, it run rails
> console but not the web.
>
> undefined method `with_acting_user' for
> #<ActiveRecord::Associations::HasManyAssociation:0x007f83b7acd4f0>
>
>
> I am not familiar with permission system and what is the correct way to do
> it.
>
> 1 class Sale < ActiveRecord::Base
> ...
> 112 def duplicate
> 113 clone = dup
> 114 clone.save!
> 115 clone.sale_products = sale_products.map {|p| p.dup }
> 116 clone
> 117 end
> ...
> 141 def duplicate_permitted?
> 142 acting_user.administrator?
> 143 end
>
> Are there some better ways to do that? I found this method only transfer the
> sale_products from original record to the new record instead of copying.
> Frustrated.
>
> And in controller, I try this:
>
> 1 class SalesController < ApplicationController
> ...
> 16 web_method :duplicate do
> 17 clone = @sale.duplicate
> 18 redirect_to sale_path(clone)
> 19 end
>
> The full trace is as following:
>
> hobo (1.4.0.pre6) lib/hobo/extensions/active_record/permissions.rb:45:in
> `insert_record_with_owner_attributes'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:128:in `block (3
> levels) in concat'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:344:in
> `add_to_target'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:127:in `block (2
> levels) in concat'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:125:in `each'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:125:in `block in
> concat'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:147:in `block in
> transaction'
> activerecord (3.1.5)
> lib/active_record/connection_adapters/abstract/database_statements.rb:194:in
> `transaction'
> activerecord (3.1.5) lib/active_record/transactions.rb:208:in `transaction'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:146:in
> `transaction'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:124:in `concat'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:312:in `block in
> replace'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:147:in `block in
> transaction'
> activerecord (3.1.5)
> lib/active_record/connection_adapters/abstract/database_statements.rb:194:in
> `transaction'
> activerecord (3.1.5) lib/active_record/transactions.rb:208:in `transaction'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:146:in
> `transaction'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:309:in `replace'
> activerecord (3.1.5)
> lib/active_record/associations/collection_association.rb:41:in `writer'
> activerecord (3.1.5)
> lib/active_record/associations/builder/association.rb:49:in `block in
> define_writers'
> app/models/sale.rb:115:in `duplicate'
> app/controllers/sales_controller.rb:17:in `block in <class:SalesController>'
> hobo (1.4.0.pre6) lib/hobo/controller/model.rb:119:in `instance_eval'
> hobo (1.4.0.pre6) lib/hobo/controller/model.rb:119:in `block (2 levels) in
> web_method'
> hobo (1.4.0.pre6) lib/hobo/model/permissions.rb:167:in `with_acting_user'
> hobo (1.4.0.pre6) lib/hobo/controller/model.rb:119:in `block in web_method'
> actionpack (3.1.5) lib/action_controller/metal/implicit_render.rb:4:in
> `send_action'
> actionpack (3.1.5) lib/abstract_controller/base.rb:167:in `process_action'
> actionpack (3.1.5) lib/action_controller/metal/rendering.rb:10:in
> `process_action'
> actionpack (3.1.5) lib/abstract_controller/callbacks.rb:18:in `block in
> process_action'
> activesupport (3.1.5) lib/active_support/callbacks.rb:461:in
> `_run__2467486077590425166__process_action__1121400367096647090__callbacks'
> activesupport (3.1.5) lib/active_support/callbacks.rb:386:in
> `_run_process_action_callbacks'
> activesupport (3.1.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
> actionpack (3.1.5) lib/abstract_controller/callbacks.rb:17:in
> `process_action'
> actionpack (3.1.5) lib/action_controller/metal/rescue.rb:17:in
> `process_action'
> actionpack (3.1.5) lib/action_controller/metal/instrumentation.rb:30:in
> `block in process_action'
> activesupport (3.1.5) lib/active_support/notifications.rb:53:in `block in
> instrument'
> activesupport (3.1.5) lib/active_support/notifications/instrumenter.rb:21:in
> `instrument'
> activesupport (3.1.5) lib/active_support/notifications.rb:53:in `instrument'
> actionpack (3.1.5) lib/action_controller/metal/instrumentation.rb:29:in
> `process_action'
> actionpack (3.1.5) lib/action_controller/metal/params_wrapper.rb:201:in
> `process_action'
> activerecord (3.1.5) lib/active_record/railties/controller_runtime.rb:18:in
> `process_action'
> actionpack (3.1.5) lib/abstract_controller/base.rb:121:in `process'
> actionpack (3.1.5) lib/abstract_controller/rendering.rb:45:in `process'
> actionpack (3.1.5) lib/action_controller/metal.rb:193:in `dispatch'
> actionpack (3.1.5) lib/action_controller/metal/rack_delegation.rb:14:in
> `dispatch'
> actionpack (3.1.5) lib/action_controller/metal.rb:236:in `block in action'
> actionpack (3.1.5) lib/action_dispatch/routing/route_set.rb:71:in `call'
> actionpack (3.1.5) lib/action_dispatch/routing/route_set.rb:71:in `dispatch'
> actionpack (3.1.5) lib/action_dispatch/routing/route_set.rb:35:in `call'
> rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
> rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in
> recognize'
> rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `optimized_each'
> rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
> rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
> actionpack (3.1.5) lib/action_dispatch/routing/route_set.rb:538:in `call'
> actionpack (3.1.5)
> lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
> rack (1.3.6) lib/rack/etag.rb:23:in `call'
> rack (1.3.6) lib/rack/conditionalget.rb:35:in `call'
> actionpack (3.1.5) lib/action_dispatch/middleware/head.rb:14:in `call'
> actionpack (3.1.5) lib/action_dispatch/middleware/params_parser.rb:21:in
> `call'
> actionpack (3.1.5) lib/action_dispatch/middleware/flash.rb:243:in `call'
> rack (1.3.6) lib/rack/session/abstract/id.rb:195:in `context'
> rack (1.3.6) lib/rack/session/abstract/id.rb:190:in `call'
> actionpack (3.1.5) lib/action_dispatch/middleware/cookies.rb:331:in `call'
> activerecord (3.1.5) lib/active_record/query_cache.rb:64:in `call'
> activerecord (3.1.5)
> lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in
> `call'
> actionpack (3.1.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block
> in call'
> activesupport (3.1.5) lib/active_support/callbacks.rb:392:in
> `_run_call_callbacks'
> activesupport (3.1.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
> actionpack (3.1.5) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
> rails-dev-tweaks (0.6.1)
> lib/rails_dev_tweaks/granular_autoload/middleware.rb:34:in `call'
> rack (1.3.6) lib/rack/sendfile.rb:101:in `call'
> actionpack (3.1.5) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
> actionpack (3.1.5) lib/action_dispatch/middleware/show_exceptions.rb:47:in
> `call'
> railties (3.1.5) lib/rails/rack/logger.rb:13:in `call'
> rack (1.3.6) lib/rack/methodoverride.rb:24:in `call'
> rack (1.3.6) lib/rack/runtime.rb:17:in `call'
> activesupport (3.1.5) lib/active_support/cache/strategy/local_cache.rb:72:in
> `call'
> rack (1.3.6) lib/rack/lock.rb:15:in `call'
> actionpack (3.1.5) lib/action_dispatch/middleware/static.rb:61:in `call'
> railties (3.1.5) lib/rails/engine.rb:456:in `call'
> railties (3.1.5) lib/rails/application.rb:143:in `call'
> rack (1.3.6) lib/rack/content_length.rb:14:in `call'
> railties (3.1.5) lib/rails/rack/log_tailer.rb:14:in `call'
> rack (1.3.6) lib/rack/handler/webrick.rb:59:in `service'
> /Users/henry/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:138:in
> `service'
> /Users/henry/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:94:in
> `run'
> /Users/henry/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/server.rb:191:in
> `block in start_thread'
>
> Thanks in advanced. Omnibus.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Hobo Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/hobousers/-/9GfrYKZ4kLIJ.
> 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/hobousers?hl=en.
--
You received this message because you are subscribed to the Google Groups "Hobo
Users" 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/hobousers?hl=en.