It worked perfect as you suggested using class method
"self.from_amazon_search_result".
Does this mean I am instantiating Product object by this new class
method rather than new()?
Sorry this is new approach to me, and I just would like to know for my
learning.

> Active Record doesn't store its attributes in instance variables, so what 
> you're doing won't work.
This was also new to me. I was building it on my belief that every
instance of AR-extending class holds its attributes in instance
variable. I will look for some specs on web just so I learn!

Thanks again Fred!

On 11月14日, 午後7:07, Frederick Cheung <[email protected]>
wrote:
> On Nov 14, 10:05 am, Ryo <[email protected]> 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" 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/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to