質問

I have this in my Active Record model:

class ItemImage < ActiveRecord::Base
    belongs_to :item, :foreign_key => :upc, :primary_key => :upc

    include HTTParty
    format :xml

    base_uri 'https://myapi.com/v2'
end

Whenever I try and do something like this in the console: ItemImage.first, Rails throws this error:

ArgumentError: wrong number of arguments (0 for 1)
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/httparty-0.13.1/lib/httparty.rb:81:in `logger'
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation/delegation.rb:37:in `block in method_missing'
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation.rb:241:in `block in scoping'
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/scoping.rb:98:in `with_scope'
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation.rb:241:in `scoping'
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation/delegation.rb:37:in `method_missing'
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/explain.rb:26:in `logging_query_plan'
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation.rb:159:in `to_a'
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:380:in `find_first'
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:122:in `first'
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/querying.rb:5:in `first'
from (irb):1
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /Users/name/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'

I'm new to the HHTParty, so I'm really not sure what's going on. I'm wondering if this is related to me using HTTParty in an active record Model. Most examples I've seen have used it outside of ActiveRecord. Any help to fix this? Thanks in advance!

役に立ちましたか?

解決

It appears to be a naming conflict between the HTTParty gem and Rails, specifically the logger component when ActiveRecord is logging the query, caused by including the HTTParty module directly into a class based on ActiveRecord.

edit to address comment:

There is no reason you can't use HTTParty in the AR model where it makes sense. You just cannot include the HTTParty methods into your model. So, while you cannot use the convenience methods to set the format and base uri, like in your sample, you should be able to use it like this:

response = HTTParty.get('https://myapi.com/v2/some-action', :format => :xml)

You will probably also need other options like :query, etc. Whether it is easier to keep the logic in your ItemImage class or extract it into a separate library class that can include HTTParty depends on your application.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top