Hi, I have a user model with attributes first_name, last_name and email. In the view I''m looping though all the users and for each user printing out the email add the full_name (full_name is a function in the user model that returns both first and last names). The email prints but the names do not and it''s really weird. I have checked in the script/console and verified that the values are actually inside the user object (I only have one user object created with the migration). What''s even weirder is that when I call User.find_by_email "blah" and it gives me back my user, the output in the console shows all the fields, shows the first_name and last_name values as well. But in the console if I do u.first_name, I get a nil return. If I do u.email, I get the "blah" return. Any idea where I can start looking for a problem? In the user.index.html.erb <% @users.each do |user| %> <tr> <td><%= user.full_name %></td> <td><%= user.email %></td> </tr> <% end %> and in my User model: def full_name @first_name + " " + @last_name end Thanks in advance -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
what''s with the @first_name, @last_name? On Jun 7, 11:18 pm, Ali <ali.akhtarz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have a user model with attributes first_name, last_name and email. > > In the view I''m looping though all the users and for each user > printing out the email add the full_name (full_name is a function in > the user model that returns both first and last names). The email > prints but the names do not and it''s really weird. I have checked in > the script/console and verified that the values are actually inside > the user object (I only have one user object created with the > migration). > > What''s even weirder is that when I call User.find_by_email "blah" and > it gives me back my user, the output in the console shows all the > fields, shows the first_name and last_name values as well. But in the > console if I do u.first_name, I get a nil return. If I do u.email, I > get the "blah" return. Any idea where I can start looking for a > problem? > > In the user.index.html.erb > > <% @users.each do |user| %> > <tr> > <td><%= user.full_name %></td> > <td><%= user.email %></td> > </tr> > <% end %> > > and in my User model: > > def full_name > @first_name + " " + @last_name > end > > Thanks in advance-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
What do you mean? What''s wrong with them? On Tue, Jun 8, 2010 at 4:55 PM, chewmanfoo <chewmanfoo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> what''s with the @first_name, @last_name? > > On Jun 7, 11:18 pm, Ali <ali.akhtarz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi, > > > > I have a user model with attributes first_name, last_name and email. > > > > In the view I''m looping though all the users and for each user > > printing out the email add the full_name (full_name is a function in > > the user model that returns both first and last names). The email > > prints but the names do not and it''s really weird. I have checked in > > the script/console and verified that the values are actually inside > > the user object (I only have one user object created with the > > migration). > > > > What''s even weirder is that when I call User.find_by_email "blah" and > > it gives me back my user, the output in the console shows all the > > fields, shows the first_name and last_name values as well. But in the > > console if I do u.first_name, I get a nil return. If I do u.email, I > > get the "blah" return. Any idea where I can start looking for a > > problem? > > > > In the user.index.html.erb > > > > <% @users.each do |user| %> > > <tr> > > <td><%= user.full_name %></td> > > <td><%= user.email %></td> > > </tr> > > <% end %> > > > > and in my User model: > > > > def full_name > > @first_name + " " + @last_name > > end > > > > Thanks in advance > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 8 June 2010 05:18, Ali <ali.akhtarzada-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> and in my User model: > > def full_name > @first_name + " " + @last_name > end >Parameters of a model aren''t assigned to instance variables, they''re accessed through method calls. So drop the "@" signs. While you''re at it, you can get rid of the string concatenation: def full_name [first_name, last_name].join(" ") end ...now, if you''re missing a first or last name, you don''t get extra white-space padding (and if you want to include ''title'' or ''middle_name'' you''re not creating n+1 strings). -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 8, 3:10 am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> def full_name > [first_name, last_name].join(" ") > end > > ...now, if you''re missing a first or last name, you don''t get extra > white-space padding (and if you want to include ''title'' or > ''middle_name'' you''re not creating n+1 strings).Never thought of that. Nice tip. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
pepe wrote:> On Jun 8, 3:10�am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> ��def full_name >> �� �[first_name, last_name].join(" ") >> ��end >> >> ...now, if you''re missing a first or last name, you don''t get extra >> white-space paddingActually, I believe you do, unless you compact the array first (posting from iPhone away from irb right now, so can''t verify).>> (and if you want to include ''title'' or >> ''middle_name'' you''re not creating n+1 strings). > > Never thought of that. Nice tip. Thanks.Indeed. I find myself using String#+ very rarely -- join and "#{ }" are both more efficient and no harder to type. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 8 June 2010 13:56, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> pepe wrote: >> On Jun 8, 3:10�am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> ��def full_name >>> �� �[first_name, last_name].join(" ") >>> ��end >>> >>> ...now, if you''re missing a first or last name, you don''t get extra >>> white-space padding > > Actually, I believe you do, unless you compact the array first (posting > from iPhone away from irb right now, so can''t verify).100% correct... sorry - my real methods do compact before the join... just not when I write them in Gmail! :-) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Oh ok. Did not know that. Thanks! After I got rid of the ''@'' part they were still returning nil. I had an attr_accessor :first_name, :last_name defined. Things worked after getting rid of that. I guess having attr_Accessor with the same names overrides the method calls or something? Thanks again! On Wed, Jun 9, 2010 at 1:26 AM, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8 June 2010 13:56, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > pepe wrote: > >> On Jun 8, 3:10�am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >>> ��def full_name > >>> �� �[first_name, last_name].join(" ") > >>> ��end > >>> > >>> ...now, if you''re missing a first or last name, you don''t get extra > >>> white-space padding > > > > Actually, I believe you do, unless you compact the array first (posting > > from iPhone away from irb right now, so can''t verify). > > 100% correct... sorry - my real methods do compact before the join... > just not when I write them in Gmail! :-) > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 9, 1:16 am, Ali Akhtarzada <ali.akhtarz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Oh ok. Did not know that. Thanks! > > After I got rid of the ''@'' part they were still returning nil. I had an > attr_accessor :first_name, :last_name defined. Things worked after getting > rid of that. I guess having attr_Accessor with the same names overrides the > method calls or something?You don''t need attr_accessor for attributes from the database - attr_accessor will overwrite (or prevent the generation of) the accessors active record provides. Fred> > Thanks again! > > > > On Wed, Jun 9, 2010 at 1:26 AM, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 8 June 2010 13:56, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > pepe wrote: > > >> On Jun 8, 3:10 am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>> def full_name > > >>> [first_name, last_name].join(" ") > > >>> end > > > >>> ...now, if you''re missing a first or last name, you don''t get extra > > >>> white-space padding > > > > Actually, I believe you do, unless you compact the array first (posting > > > from iPhone away from irb right now, so can''t verify). > > > 100% correct... sorry - my real methods do compact before the join... > > just not when I write them in Gmail! :-) > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.