On Nov 14, 10:05 am, Ryo <
ryoichiro.kam...@gmail.com> wrote:
> and actual code for the model class is:
> ----------------------------------------------------------------
> class Product < ActiveRecord::Base
> has_many :inventories
>
> def initialize(item)
> super
> @amazon_asin = item.get('asin')
> @manufacturer = item.get('itemattributes/manufacturer')
> @name = item.get('itemattributes/title')
> @releasedate = item.get('itemattributes/releasedate')
> @amazon_url = item.get('detailpageurl')
> @amazon_image = item.get_hash('smallimage')
> end
> ----------------------------------------------------------------
> item is the result from Amazon API search result. I am trying to
The reason this isn't working is that
super
does not mean 'call the superclass implementation with no
arguments' (which is fine in this case - Active Record just won't set
any attributes). It means 'call the super class implementation with
the same arguments', so you're passing this an amazon search result
object to a method expecting a hash. You can force the superclass to
be called with no arguments by doing super().
I assume that you're trying to set activerecord attributes for the
title and so on. If so, use the accessor functions (ie
self.amazon_asin = ... ). Active Record doesn't store its attributes
in instance variables, so what you're doing won't work.
Lastly while you can make the above work, I think that you'll be
making life for yourself more difficult by requiring that Products be
initialized with an amazon search result object (eg for creating
objects in tests). another approach is to leave initialize alone and
write
class Product < ...
def self.from_amazon_search_result(item)
new :amazon_asin => item.get('asin'), :manufacturer => item.get
('itemattributes/manufacturer') , ...
end
end
Fred