codemancers / invoicing

Ruby invoicing framework gem
http://invoicing.c9s.dev/
MIT License
265 stars 40 forks source link

undefined method `val' for #<Arel::Nodes::BindParam:0x0000000625ef08> #49

Closed OtherCroissant closed 9 years ago

OtherCroissant commented 9 years ago

Hi,

I followed the steps that you suggested http://invoicing.codemancers.com/, but unfortunately I cannot get it working. Once I want to build an object that inherits the Invoice model, I get the following error: undefined method `val' for #Arel::Nodes::BindParam:0x0000000625ef08

I'm using Rails 4.2 and the stack trace is as follows:

activerecord (4.2.0) lib/active_record/relation.rb:572:in `block (2 levels) in where_values_hash'
activerecord (4.2.0) lib/active_record/relation.rb:568:in `fetch'
activerecord (4.2.0) lib/active_record/relation.rb:568:in `block in where_values_hash'
activerecord (4.2.0) lib/active_record/relation.rb:566:in `map'
activerecord (4.2.0) lib/active_record/relation.rb:566:in `where_values_hash'
activerecord (4.2.0) lib/active_record/relation.rb:579:in `scope_for_create'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:503:in `create_scope'
activerecord (4.2.0) lib/active_record/associations/association.rb:168:in `initialize_attributes'
activerecord (4.2.0) lib/active_record/associations/association.rb:248:in `block in build_record'
activerecord (4.2.0) lib/active_record/core.rb:282:in `initialize'
invoicing (1.0.1) lib/invoicing/ledger_item.rb:417:in `initialize'
activerecord (4.2.0) lib/active_record/inheritance.rb:61:in `new'
activerecord (4.2.0) lib/active_record/inheritance.rb:61:in `new'
activerecord (4.2.0) lib/active_record/reflection.rb:131:in `build_association'
activerecord (4.2.0) lib/active_record/associations/association.rb:247:in `build_record'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:136:in `build'
activerecord (4.2.0) lib/active_record/associations/collection_proxy.rb:254:in `build'
app/controllers/crud_controller.rb:82:in `build_member'
app/controllers/crud_controller.rb:16:in `index'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:308:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:308:in `block (2 levels) in halting'
app/controllers/application_controller.rb:84:in `set_current_user'
activesupport (4.2.0) lib/active_support/callbacks.rb:427:in `block in make_lambda'
activesupport (4.2.0) lib/active_support/callbacks.rb:307:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:307:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:308:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:308:in `block (2 levels) in halting'
activesupport (4.2.0) lib/active_support/core_ext/time/zones.rb:48:in `use_zone'
app/controllers/application_controller.rb:64:in `set_time_zone'
activesupport (4.2.0) lib/active_support/callbacks.rb:427:in `block in make_lambda'
activesupport (4.2.0) lib/active_support/callbacks.rb:307:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:307:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
bullet (4.14.4) lib/bullet/rack.rb:12:in `call'
http_accept_language (2.0.2) lib/http_accept_language/middleware.rb:13:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.6.0) lib/rack/etag.rb:24:in `call'
rack (1.6.0) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.0) lib/rack/head.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.0) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.0) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
web-console (2.0.0) lib/action_dispatch/debug_exceptions.rb:18:in `middleware_call'
web-console (2.0.0) lib/action_dispatch/debug_exceptions.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.0) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.0) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.0) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.0) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
rack (1.6.0) lib/rack/lock.rb:17:in `call'
rack (1.6.0) lib/rack/content_length.rb:15:in `call'
rack (1.6.0) lib/rack/handler/webrick.rb:89:in `service'
/home/christiaan/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/christiaan/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/christiaan/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

Could you point me in the right direction?

iffyuva commented 9 years ago

@CUnknown thanks for this bug report. I will get back to you on this.

iffyuva commented 9 years ago

looks like this issue is not entirely related to this gem. @CUnknown can you post code showing how you inherited from Invoice model, and the code in CrudController#build_member ?

OtherCroissant commented 9 years ago

Thanks for your reply! The invoice classes are looking like this:

class Invoice < InvoicingLedgerItem
  acts_as_invoice
end

class ClientInvoice < Invoice
  belongs_to :organisation
  belongs_to :recipient, class_name: 'Client'
  belongs_to :author, class_name: 'User'
end

CrudController#build_member is looking like:

collection.build(member_params)

where member_params are the fetched permitted parameters and the collection is organisation.client_invoices

My Organisation model (and my Client model) has a has_many relation:

has_many :client_invoices, dependent: :destroy

I Found something interesting here. When I open the rails console and perform ClientInvoices.all, I get the empty set [], which is correct. When I perform Organisation.find(1).client_invoices I get the following:

[6] pry(main)> Organisation.find(1).client_invoices
  Organisation Load (1.1ms)  SELECT  "organisations".* FROM "organisations" WHERE "organisations"."id" = $1 LIMIT 1  [["id", 1]]
PG::UndefinedColumn: ERROR:  column invoicing_ledger_items.organisation_id does not exist
LINE 1: ...ing_ledger_items"."type" IN ('ClientInvoice') AND "invoicing...
                                                             ^
: SELECT "invoicing_ledger_items".* FROM "invoicing_ledger_items" WHERE "invoicing_ledger_items"."type" IN ('ClientInvoice') AND "invoicing_ledger_items"."organisation_id" = $1
=> #<ClientInvoice::ActiveRecord_Associations_CollectionProxy:0x48dc5bc>

Which is weird, because it looks for the column organisation_id in the invoicing_ledger_items table, and not in the client_invoices table (which has those references).

Kind Regards,

Christiaan

iffyuva commented 9 years ago

oh okie. so you dont have proper foreign key associations, try this:

class Organisation < AR::Base
  has_many :client_invoices, dependent: :destroy, foreign_key: :sender_id
end

@CUnknown can you check with this code?

OtherCroissant commented 9 years ago

It looks like I got it working. I was creating a model which handled the relations with the sender and the receiver myself, but your gem already stores these references.

I also had a question about the functionality. I want to create a invoice controller where the user can add invoice items on the fly (with a nested forms construction). Do you think that this is possible? Another feature that I would like to have is the ability to save concept invoices. In your instructions, you mention that the invoices are immuatable, which is the desired behaviour when the created invoice has an invoice number attached to it. (Our tax authorities require the invoice numbering to be complete, so no invoice number may be missing from the administration). But if we want to be able to create concept invoices, these should not get an invoice number and those should not be immutable yet. Is it possible to create such a system with your gem?

iffyuva commented 9 years ago

ability to create invoices on the fly is possible. please open a new issue and explain what is required. 'invoices are immutable' is not entirely true. its afterall bunch of entries in database, and you can always change them, you have to write more code.