i''m trying to understand navigating activerecord relationships. For example I''ve got: class Product < ActiveRecord::Base has_many :line_items end class LineItem < ActiveRecord::Base belongs_to :product has_one :price end class Price < ActiveRecord::Base belongs_to :line_item end ok so Price isn''t the best example but the point is LineItem has_one Price. so I''ve passed a Product object from the controller into a view and i''m iterating over it like thus: <% for lineitem in @product.line_items %> then I want to write a conditional statement to test if there''s a price object for that lineitem so: <% if lineitem.price %> print something <% end %> the problem is the "if" line raises: (eval):1:in `compute_type'': compile error (eval):1: parse error, unexpected tINTEGER Object::1 so what am I doing wrong? thanks --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
creativetags wrote:> i''m trying to understand navigating activerecord relationships. For > example I''ve got: > > class Product < ActiveRecord::Base > has_many :line_items > end > class LineItem < ActiveRecord::Base > belongs_to :product > has_one :price > end > class Price < ActiveRecord::Base > belongs_to :line_item > end > > ok so Price isn''t the best example but the point is LineItem has_one > Price. > so I''ve passed a Product object from the controller into a view and i''m > iterating over it like thus: > > <% for lineitem in @product.line_items %> > > then I want to write a conditional statement to test if there''s a price > object for that lineitem so: > > <% if lineitem.price %> > print something > <% end %> > > the problem is the "if" line raises: > > (eval):1:in `compute_type'': compile error > (eval):1: parse error, unexpected tINTEGER > Object::1 > > so what am I doing wrong? > > thanksI suspect it''s because ''price'' isn''t a boolean. Perhaps unles lineitem.price.nil? unless lineitem.price.blank? Alan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I suspect it''s because ''price'' isn''t a boolean. Perhaps > > unless lineitem.price.nil? > > unless lineitem.price.blank?sorry still giving the same error. mark --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
your logic seems right as the only things that will return false are the boolean false and nil. You may want to check to make sure that you have the correct columns in your database. the table prices should have line_item_id and line_items should contain product_id. But even that probably wouldn''t return the error that you''re receiving. Other than that all I can think of is to check the rest of your code leading to that line. On 12/20/06, creativetags <creativetags-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > I suspect it''s because ''price'' isn''t a boolean. Perhaps > > > > unless lineitem.price.nil? > > > > unless lineitem.price.blank? > > sorry still giving the same error. > > mark > > > > >-- Mike Weber Web Developer UW-Eau Claire --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ok i just checked all the code - the db seems to be setup correctly with foreign keys. I tested the original ''if'' statement i proposed and the condition returns false (or nil) skipping the block if there is no price object associated with the lineitem in the db. I still get the error about unexpected tINTEGER if there is an associated price. Anyone know what that means? what it should be? thanks --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-20 17:27 UTC
Re: Navigating model relationships
Hi -- On Wed, 20 Dec 2006, Alan Francis wrote:> > creativetags wrote: >> i''m trying to understand navigating activerecord relationships. For >> example I''ve got: >> >> class Product < ActiveRecord::Base >> has_many :line_items >> end >> class LineItem < ActiveRecord::Base >> belongs_to :product >> has_one :price >> end >> class Price < ActiveRecord::Base >> belongs_to :line_item >> end >> >> ok so Price isn''t the best example but the point is LineItem has_one >> Price. >> so I''ve passed a Product object from the controller into a view and i''m >> iterating over it like thus: >> >> <% for lineitem in @product.line_items %> >> >> then I want to write a conditional statement to test if there''s a price >> object for that lineitem so: >> >> <% if lineitem.price %> >> print something >> <% end %> >> >> the problem is the "if" line raises: >> >> (eval):1:in `compute_type'': compile error >> (eval):1: parse error, unexpected tINTEGER >> Object::1 >> >> so what am I doing wrong? >> >> thanks > > I suspect it''s because ''price'' isn''t a boolean. Perhaps > > unles lineitem.price.nil? > > unless lineitem.price.blank?That shouldn''t matter; every Ruby expression has a boolean value of true or false, for purposes of conditional evaluation. David -- Q. What''s a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-20 17:28 UTC
Re: Navigating model relationships
Hi -- On Wed, 20 Dec 2006, creativetags wrote:> > i''m trying to understand navigating activerecord relationships. For > example I''ve got: > > class Product < ActiveRecord::Base > has_many :line_items > end > class LineItem < ActiveRecord::Base > belongs_to :product > has_one :price > end > class Price < ActiveRecord::Base > belongs_to :line_item > end > > ok so Price isn''t the best example but the point is LineItem has_one > Price. > so I''ve passed a Product object from the controller into a view and i''m > iterating over it like thus: > > <% for lineitem in @product.line_items %> > > then I want to write a conditional statement to test if there''s a price > object for that lineitem so: > > <% if lineitem.price %> > print something > <% end %> > > the problem is the "if" line raises: > > (eval):1:in `compute_type'': compile error > (eval):1: parse error, unexpected tINTEGER > Object::1 > > so what am I doing wrong?Do you have an <% end %> for the "for" loop as well as the "if"? David -- Q. What''s a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Do you have an <% end %> for the "for" loop as well as the "if"?yeah sorry didn''t include it up there for speed. thanks anyway. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
did you set up the database using migrations or sql statements? it almost seems like a type mismatch: it''s getting an integer when it''s expecting a float for example. On 12/20/06, creativetags <creativetags-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > Do you have an <% end %> for the "for" loop as well as the "if"? > > yeah sorry didn''t include it up there for speed. thanks anyway. > > > > >-- Mike Weber Web Developer UW-Eau Claire --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
i set up the db with migrations. i can''t see any wrong types in the db schema that I know of. It''s strange because it''s not actually returning an integer to lineitem.price it''s throwing an error saying it''s returning an integer. Is it saying that because it''s expecting a boolean for the ''if'' condition? If so how do I write a condition to test if a lineitem has a price? --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
>From what you described, the error is a compile error and has nothingto do with object type at runtime. I would double check your source code to make sure you don''t have an integer on that line somewhere (like a 1 for the letter i). If your still having a problem post the exact source code and someone will be able to figure it out. -Paul --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
i think i''m getting somewhere now - ''type'' appears to be reserved for single table inheritance. the things you don''t think of when you''re a newbie :) thanks again for all your help --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It seems that "type" is a forbidden column name in Ruby''s ActiveRecord. More details here: http://nhw.pl/wp/2006/05/26/column-names-restrictions/ Hope that helps. --Stephen -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---