I''m driving myself nuts with this annoying bug that I just don''t get. I have a users table/model and a tickets table/model. Something like this: users ( id int primary key auto_increment, first_name varchar(20), last_name varchar(20) ) tickets ( id primary key auto_increment, user_id int, created_on datetime ) ticket.rb belongs_to :users user.rb has_many :tickets The problem I''m having is trying to access the user data when showing the tickets like so: <% @tickets.each do |ticket| %> <%= ticket.user.id %> <%= ticket.user.first_name %> <%= ticket.user.inspect %> <% end %> When I access ticket.user.id there''s no problem, but when I try ticket.user.first_name it throws an undefined method `last_name'' for nil:NilClass exception. When I use ticket.user.inspect all the right data is there -- so what am I not getting about this? What''s missing? And I can''t seem to find, although I know I came across it before, but how can I add a virtual accessor to the class like def self.full_name return self.first_name << " " << self.last_name end as I''m getting the same errors when I use the above method. Thanks, -- Craig Beck http://blog.rebelplatoon.com http://luckybonza.com AIM: Kreiggers _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 5/24/05, Craig Beck <craigbeck-sUNYKzuJffo33s2pWanAEQ@public.gmane.org> wrote:> I''m driving myself nuts with this annoying bug that I just don''t get. > > I have a users table/model and a tickets table/model. Something like this: > > users ( > id int primary key auto_increment, > first_name varchar(20), > last_name varchar(20) > ) > > tickets ( > id primary key auto_increment,Probably unrelated, but try giving that column type ''int'' like the users table.> user_id int, > created_on datetime > ) > > ticket.rb > belongs_to :usersbelongs_to :user> > user.rb > has_many :tickets > > The problem I''m having is trying to access the user data when showing the > tickets like so: > > <% @tickets.each do |ticket| %> > <%= ticket.user.id %> > <%= ticket.user.first_name %> > <%= ticket.user.inspect %> > <% end %> > > > When I access ticket.user.id there''s no problem, but when I try > ticket.user.first_name it throws an undefined method `last_name'' for > nil:NilClass exception. When I use ticket.user.inspect all the right data is > there -- so what am I not getting about this? What''s missing?You sure that ticket.user.id is giving you the RIGHT id?> > And I can''t seem to find, although I know I came across it before, but how > can I add a virtual accessor to the class like > > def self.full_name > return self.first_name << " " << self.last_name > end > > as I''m getting the same errors when I use the above method.You probably don''t want to make a class function. So you''d have def full_name return first_name << " " << last_name # or return self.first_name << " " << self.last_name # not sure which is better end And use it like user = User.find(.... ) user.full_name Joe
On May 24, 2005, at 12:42 AM, Joe Van Dyk wrote:> On 5/24/05, Craig Beck <craigbeck-sUNYKzuJffo33s2pWanAEQ@public.gmane.org> wrote: > >> I''m driving myself nuts with this annoying bug that I just don''t get. >> >> I have a users table/model and a tickets table/model. Something >> like this: >> >> users ( >> id int primary key auto_increment, >> first_name varchar(20), >> last_name varchar(20) >> ) >> >> tickets ( >> id primary key auto_increment, >> > > Probably unrelated, but try giving that column type ''int'' like the > users table. >> belongs_to :userSorry, I should have specified -- pseudo code. Not exactly what the sql is to create the table, but he general idea anyway.>> >> user.rb >> has_many :tickets >> >> The problem I''m having is trying to access the user data when >> showing the >> tickets like so: >> >> <% @tickets.each do |ticket| %> >> <%= ticket.user.id %> >> <%= ticket.user.first_name %> >> <%= ticket.user.inspect %> >> <% end %> >> >> >> When I access ticket.user.id there''s no problem, but when I try >> ticket.user.first_name it throws an undefined method `last_name'' for >> nil:NilClass exception. When I use ticket.user.inspect all the >> right data is >> there -- so what am I not getting about this? What''s missing? >> > > You sure that ticket.user.id is giving you the RIGHT id?Yes -- the id is correct. As I said, when I use ticket.user.inspect, the correct values are there but if I try to access them directly then I get an exception. The exception is when I access the id -- which works just fine, but isn''t particularly helpful.>> And I can''t seem to find, although I know I came across it before, >> but how >> can I add a virtual accessor to the class like >> >> def self.full_name >> return self.first_name << " " << self.last_name >> end >> >> as I''m getting the same errors when I use the above method. > > You probably don''t want to make a class function. > > So you''d have > > def full_name > return first_name << " " << last_name > # or return self.first_name << " " << self.last_name > # not sure which is better > end > > And use it like > user = User.find(.... ) > user.full_nameOh, so I have it backwards? def self.some_method makes a class mathod and def some_method makes an instance method? -- Craig Beck http://blog.rebelplatoon.com http://luckybonza.com AIM: Kreiggers _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On May 24, 2005, at 3:07 AM, Craig Beck wrote:> > On May 24, 2005, at 12:42 AM, Joe Van Dyk wrote: > >> On 5/24/05, Craig Beck <craigbeck-sUNYKzuJffo33s2pWanAEQ@public.gmane.org> wrote: >> >>> I''m driving myself nuts with this annoying bug that I just don''t >>> get. >>> >>> I have a users table/model and a tickets table/model. Something >>> like this: >>> >>> users ( >>> id int primary key auto_increment, >>> first_name varchar(20), >>> last_name varchar(20) >>> ) >>> >>> tickets ( >>> id primary key auto_increment, >>> >> >> Probably unrelated, but try giving that column type ''int'' like the >> users table. >> > >> belongs_to :user > > Sorry, I should have specified -- pseudo code. Not exactly what the > sql is to create the table, but he general idea anyway. > >>> >>> user.rb >>> has_many :tickets >>> >>> The problem I''m having is trying to access the user data when >>> showing the >>> tickets like so: >>> >>> <% @tickets.each do |ticket| %> >>> <%= ticket.user.id %> >>> <%= ticket.user.first_name %> >>> <%= ticket.user.inspect %> >>> <% end %> >>> >>> >>> When I access ticket.user.id there''s no problem, but when I try >>> ticket.user.first_name it throws an undefined method `last_name'' for >>> nil:NilClass exception. When I use ticket.user.inspect all the >>> right data is >>> there -- so what am I not getting about this? What''s missing? >>> >> >> You sure that ticket.user.id is giving you the RIGHT id? > > Yes -- the id is correct. As I said, when I use > ticket.user.inspect, the correct values are there but if I try to > access them directly then I get an exception. The exception is when > I access the id -- which works just fine, but isn''t particularly > helpful.Okay, just realised my bonehead mistake -- I didn''t require "user" in the ticket_controller.rb file. Problem solved. Amazing what taking a break and getting some rest from pounding my head into a wall will do. -- Craig Beck http://blog.rebelplatoon.com http://luckybonza.com AIM: Kreiggers _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 5/24/05, Craig Beck <craigbeck-sUNYKzuJffo33s2pWanAEQ@public.gmane.org> wrote:> > > On May 24, 2005, at 12:42 AM, Joe Van Dyk wrote: > > On 5/24/05, Craig Beck <craigbeck-sUNYKzuJffo33s2pWanAEQ@public.gmane.org> wrote: > > I''m driving myself nuts with this annoying bug that I just don''t get. > > I have a users table/model and a tickets table/model. Something like this: > > users ( > id int primary key auto_increment, > first_name varchar(20), > last_name varchar(20) > ) > > tickets ( > id primary key auto_increment, > > Probably unrelated, but try giving that column type ''int'' like the users > table. > > > > belongs_to :user > > Sorry, I should have specified -- pseudo code. Not exactly what the sql is > to create the table, but he general idea anyway.Are you using "belongs_to :user" or "belongs_to :users"? I think you want to the singular version.> Oh, so I have it backwards? > > def self.some_method makes a class mathod and def some_method makes an > instance method?Correct. You could use def User.full_name instead to make a class method, I think.
On May 24, 2005, at 3:53 PM, David Teare wrote:> >> You sure that ticket.user.id is giving you the RIGHT id? > > What Joe meant by this is that in ruby nil.id == 4, which can be > very confusing. Add the following code to application_helper.rb to > ensure this doesn''t trick you again in the future: > > def nil.id() raise(ArgumentError, "You are calling nil.id! > This will result in ''4''!") endNot sure I''m following you.... are you saying that NilClass has an id attribute that could return 4? That seems a bit odd. But anyway, I don''t believe that''s what is happening as when I use ticket.user.id and it returns 4, that''s a valid user.id in the database. If I use ticket.user.inspect then I get what I''d expect from the database data ex: #<User:0x2325afc @attributes={"id"=>"1", "first_name"=>"Craig", "last_name"=>"Beck", "active"=>"1"} which is exactly right. Where I''m confused is without using require "user" I can access the ticket.user.id attribute just fine, but it would throw an "undefined method `last_name'' for nil:NilClass" exception if i tried to access ticket.user.last_name (yes, last_name is a column in the db schema). But if I just inspect the ticket.user object, all the correct attributes are there -- but it doesn''t think it''s a class of its own? (hence the NilClass exception) If I require "user" in the ticket_controller.rb file then everything works fine and Rails treats tickets.user as a full fledged User object. So I''m guessing Rails is really smart, but not smart enough to know when I''m associating to another table that there''s a model for that table? Is there another way I should specify the User model to the ticket_controller? perhaps model :user, :ticket, :note when I know tickets have notes and belong to a user? -- Craig Beck http://blog.rebelplatoon.com http://luckybonza.com AIM: Kreiggers _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
>> You sure that ticket.user.id is giving you the RIGHT id? What Joe meant by this is that in ruby nil.id == 4, which can be very confusing. Add the following code to application_helper.rb to ensure this doesn''t trick you again in the future: def nil.id() raise(ArgumentError, "You are calling nil.id! This will result in ''4''!") end Joe Van Dyk wrote: On 5/24/05, Craig Beck wrote: I''m driving myself nuts with this annoying bug that I just don''t get. I have a users table/model and a tickets table/model. Something like this: users ( id int primary key auto_increment, first_name varchar(20), last_name varchar(20) ) tickets ( id primary key auto_increment, Probably unrelated, but try giving that column type ''int'' like the users table. user_id int, created_on datetime ) ticket.rb belongs_to :users belongs_to :user user.rb has_many :tickets The problem I''m having is trying to access the user data when showing the tickets like so: <% @tickets.each do |ticket| %> <%= ticket.user.id %> <%= ticket.user.first_name %> <%= ticket.user.inspect %> <% end %> When I access ticket.user.id there''s no problem, but when I try ticket.user.first_name it throws an undefined method `last_name'' for nil:NilClass exception. When I use ticket.user.inspect all the right data is there -- so what am I not getting about this? What''s missing? You sure that ticket.user.id is giving you the RIGHT id? And I can''t seem to find, although I know I came across it before, but how can I add a virtual accessor to the class like def self.full_name return self.first_name << " " << self.last_name end as I''m getting the same errors when I use the above method. You probably don''t want to make a class function. So you''d have def full_name return first_name << " " << last_name # or return self.first_name << " " << self.last_name # not sure which is better end And use it like user = User.find(.... ) user.full_name Joe _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Craig, RE: nil.id <http://nil.id>, check out the gotcha''s page in the wiki: http://wiki.rubyonrails.com/rails/show/Gotcha On 5/25/05, Craig Beck <craigbeck-sUNYKzuJffo33s2pWanAEQ@public.gmane.org> wrote:> > > On May 24, 2005, at 3:53 PM, David Teare wrote: > > >> You sure that ticket.user.id <http://ticket.user.id> is giving you the > RIGHT id? > > What Joe meant by this is that in ruby nil.id <http://nil.id> == 4, which > can be very confusing. Add the following code to application_helper.rb to > ensure this doesn''t trick you again in the future: > > def nil.id() raise(ArgumentError, "You are calling nil.id <http://nil.id>! > This will result in ''4''!") end > > > Not sure I''m following you.... are you saying that NilClass has an idattribute that could return 4? That seems a bit odd. > > But anyway, I don''t believe that''s what is happening as when I use > ticket.user.id <http://ticket.user.id> and it returns 4, that''s a valid > user.id <http://user.id> in the database. If I use ticket.user.inspectthen I get what I''d expect from the database data ex: > > #<User:0x2325afc @attributes={"id"=>"1", "first_name"=>"Craig", > "last_name"=>"Beck", "active"=>"1"} > > which is exactly right. > > Where I''m confused is without using require "user" I can access the > ticket.user.id <http://ticket.user.id> attribute just fine, but it would > throw an "undefined method `last_name'' for nil:NilClass" exception if i > tried to access ticket.user.last_name (yes, last_name is a column in the > db schema). But if I just inspect the ticket.user object, all the correct > attributes are there -- but it doesn''t think it''s a class of its own? (hence > the NilClass exception) > > If I require "user" in the ticket_controller.rb file then everything works > fine and Rails treats tickets.user as a full fledged User object. So I''m > guessing Rails is really smart, but not smart enough to know when I''m > associating to another table that there''s a model for that table? > > Is there another way I should specify the User model to the > ticket_controller? perhaps model :user, :ticket, :note when I know tickets > have notes and belong to a user? > > -- > Craig Beck > http://blog.rebelplatoon.com > http://luckybonza.com > AIM: Kreiggers > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Jordan Brock Spin Technologies www.spintech.com.au <http://www.spintech.com.au> _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails