Hi all,
It''s my first attempt at setting up eager associations and running
into some problems. Basically, I have three tables:
Products
id; category_id; asin; product; prd_url; active; created_on; updated_on
Categories
id; category; created_on, updated_on
Salesranks
id, product_id, rank, nextupdate_on; created_on; updated_on
In short, I have a list of products, each assigned to a category, and
a table of sales rankings for each product updated every 4 hours.
My models to describe this are:
class Product < ActiveRecord::Base
has_many :salesranks, :order => "created_on"
belongs_to :category
class Category < ActiveRecord::Base
has_many :products
class Salesrank < ActiveRecord::Base
belongs_to :product
In my product controller, I am performing the following query:
@pops = Product.find(:all,
:conditions => "active = 1 and products.id =
26",
:include => [ :salesranks, :category ])
The code in my view to generate a table looks like:
<snip>
<% @pops.each do |pop| %>
<tr>
<td><%= pop.product %></td>
<td><%= pop.created_on %></td>
<td><%= pop.category %>
</tr>
</snip> - this works fine.
The trouble I am having is if I try to include the name of the
category or the salesrank using something like:
<td><%= pop.salesrank.rank %>
I get the following error message.
undefined method `salesrank'' for #<Product:0x3bbaaf8>
I am not sure exactly of how to address fields from the joined tables.
(perhaps I''m overlooking something in the api).
What am I doing wrong?
Thanks,
Steve
Thanks, Chris. Tried your suggestion but still getting this error:
undefined method `salesrank'' for #<Product:0x3bb8db8>
I did a pop.inspect like this:
<% @pops.each do |pop| %>
<%= pop.inspect %>
<tr>
to see what it looked like and this is what I got in my html source:
#<Product:0x3b33900 @salesranks=[#<Salesrank:0x3b32610
@attributes={"created_on"=>"2005-05-03 21:18:23",
"updated_on"=>"2005-05-03
21:18:23", "product_id"=>"26",
"id"=>"21", "rank"=>"214",
"nextupdate_on"=>"2005-05-04 01:18:23"}>],
@category=#<Category:0x3b321c0
@attributes={"created_on"=>"2005-05-03 21:18:22",
"name"=>"DVD",
"updated_on"=>"2005-05-03 21:18:22",
"id"=>"1"}>,
@attributes={"created_on"=>"2005-05-03 21:18:22",
"prdURL"=>"
http://www.amazon.com/exec/obidos/ASIN/B00005V3Z4/calibanorg-20?dev-t=",
"prdURL_image"=>"
http://images.amazon.com/images/P/B00005V3Z4.01.THUMBZZZ.jpg",
"updated_on"=>"2005-05-03 21:18:22",
"product"=>"Donnie Darko",
"asin"=>"B00005V3Z4", "id"=>"26",
"category_id"=>1, "user_id"=>nil,
"active"=>"1"}>
That was with a controller method that looked like this:
@pops = Product.find(:all,
:conditions => "active = 1 and products.id = 26",
:include => [ :salesranks, :category ])
I noticed the ''#'' where ''created_on'' should
be at the very first part of the
inspect output. I thought maybe the salesrank model wasn''t right, so I
made
my model look like this:
@pops = Product.find(:all,
:conditions => "active = 1 and products.id = 26",
:include => [ :category ])
Which resulted in the inspect html source output looking like this:
#<Product:0x3bb1bb8 @category=#<Category:0x3bb1570
@attributes={"name"=>"DVD",
"created_on"=>"2005-05-03 21:18:22",
"updated_on"=>"2005-05-03 21:18:22",
"id"=>"1"}>,
@attributes={"created_on"=>"2005-05-03 21:18:22",
"prdURL"=>"
http://www.amazon.com/exec/obidos/ASIN/B00005V3Z4/calibanorg-20?dev-t=",
"prdURL_image"=>"
http://images.amazon.com/images/P/B00005V3Z4.01.THUMBZZZ.jpg",
"updated_on"=>"2005-05-03 21:18:22",
"product"=>"Donnie Darko",
"asin"=>"B00005V3Z4", "id"=>"26",
"category_id"=>1, "user_id"=>nil,
"active"=>"1"}>
What''s up with the # appearing at the very beginning? Any clues as to
how I
should reference the rank of salesrank or the category of category?
Thanks very much,
Steve
On 5/11/05, Chris Trimble <trimbo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On 5/11/05, Steve Odom
<steve.odom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > class Product < ActiveRecord::Base
> > has_many :salesranks, :order => "created_on"
> > <td><%= pop.salesrank.rank %>
> > undefined method `salesrank'' for #<Product:0x3bbaaf8>
>
> has_many creates an array. Try pop.salesranks[0].rank
>
> - Chris
>
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
On 5/11/05, Steve Odom <steve.odom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks, Chris. Tried your suggestion but still getting this error: > undefined method `salesrank'' for #<Product:0x3bb8db8>Seems like something is still calling salesrank instead of salesranks. You may want to try testing your query using the console instead of using ERB to debug. Run "ruby script/console" from the top of your app''s tree.> I noticed the ''#'' where ''created_on'' should be at the very first part of the > inspect output. What''s up with the # appearing at the very beginning?This is normal -- all non-std-library class instances in Ruby return this on inspection. In console, try making an empty class called "Mytest" and instancing it. You''ll get #<Mytest:0xdeadbeef> (i.e. some hex location).> Any clues as to how I > should reference the rank of salesrank or the category of category?>From the inspect you posted, the category attribute has been changedto "name". So you would use pop.category.name to access the "category" attribute instead of pop.category.category - Chris
I got it working Chris. Thanks for your help. Here is what I learned: Since product "has_many" salesranks, salesranks table references should be plural. Further, references like pop.salesrank.colname won''t work with a has_many modeled table because it will return more than one item. so the reference has to tell it only give me one - the last one or the first one. such as: pop.salesrank.last.colname. Steve On 5/12/05, Chris Trimble <trimbo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5/11/05, Steve Odom <steve.odom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Thanks, Chris. Tried your suggestion but still getting this error: > > undefined method `salesrank'' for #<Product:0x3bb8db8> > > Seems like something is still calling salesrank instead of salesranks. > You may want to try testing your query using the console instead of > using ERB to debug. Run "ruby script/console" from the top of your > app''s tree. > > > I noticed the ''#'' where ''created_on'' should be at the very first part of the > > inspect output. What''s up with the # appearing at the very beginning? > > This is normal -- all non-std-library class instances in Ruby return > this on inspection. In console, try making an empty class called > "Mytest" and instancing it. You''ll get #<Mytest:0xdeadbeef> (i.e. > some hex location). > > > Any clues as to how I > > should reference the rank of salesrank or the category of category? > > >From the inspect you posted, the category attribute has been changed > to "name". So you would use pop.category.name to access the > "category" attribute instead of pop.category.category > > - Chris > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >