Hello All, I am very new to Ruby on Rails and I have a quick question for you all, which is as follows: I have this following line of code in an partial that renders itself on the posts/index view: Created By (User): <%= User.find(:all, :select => ''name'', :conditions => ["id = ?", 1]) %> (I am hard-coding that ''1'' there) Now, when I run the server; the above line of code displays just the ''#'' symbol.. nothing else (no name is displayed :-( ): Created By (User): # I have the following tables: User, Post & Comment. For your reference, I am also pasting here the contents of my ''User'' table that I got from my console:>> User.find(:all)=> [#<User id: 1, name: "Ron", created_at: "2010-03-15 02:45:28", updated_at: "2010-03-15 02:45:28">, #<User id: 2, name: "David", created_at: "2010-03-15 21:13:22", updated_at: "2010-03-15 21:13:22">, #<User id: 3, name: "Tom", created_at: "2010-03-15 21:38:31", updated_at: "2010-03-15 21:38:31">, #<User id: 4, name: "John", created_at: "2010-03-15 21:38:43", updated_at: "2010-03-15 21:38:43">, #<User id: 5, name: "Harry", created_at: "2010-03-15 21:38:50", updated_at: "2010-03-15 21:38:50">, #<User id: 6, name: "Martin", created_at: "2010-03-15 21:38:57", updated_at: "2010-03-15 21:38:57">] Can you please let me me know if I am missing something here or if I am doing anything wrong? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 16, 5:41 pm, Rohit Shinde <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello All, > > I am very new to Ruby on Rails and I have a quick question for you all, > which is as follows: > > I have this following line of code in an partial that renders itself on > the posts/index view: > Created By (User): <%= User.find(:all, :select => ''name'', :conditions => > ["id = ?", 1]) %> > (I am hard-coding that ''1'' there) > > Now, when I run the server; the above line of code displays just the ''#'' > symbol.. nothing else (no name is displayed :-( ): > Created By (User): # >That''s because all that is being stuck in the view is something like #<User id: 1, name: "Ron", created_at: "2010-03-15 02:45:28", updated_at: "2010-03-15 02:45:28"> Which isn''t legal html (you can verify this by viewing the html source in your browser) 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 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.
Thanks Fred.... when I view d page source.. I see something like this: Created By (User): #<User:0x1042bbec0> I guess this is hex representation of the memory point to the following object <User id: 1, name: "Ron", created_at: "2010-03-15 02:45:28",> updated_at: "2010-03-15 02:45:28">Can you please advice me a way to extract just the name from there? Frederick Cheung wrote:> On Mar 16, 5:41�pm, Rohit Shinde <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> Now, when I run the server; the above line of code displays just the ''#'' >> symbol.. nothing else (no name is displayed :-( ): >> Created By (User): # >> > > That''s because all that is being stuck in the view is something like > > #<User id: 1, name: "Ron", created_at: "2010-03-15 02:45:28", > updated_at: "2010-03-15 02:45:28"> > > Which isn''t legal html (you can verify this by viewing the html source > in your browser) > > Fred-- 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 16 March 2010 19:33, Rohit Shinde <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Can you please advice me a way to extract just the name from there?This will give you what you want: <%= User.find(:all, :select => ''name'', :conditions => ["id = ?", 1]).name %> ...but there are so many poor and bad-practice things going on in that line.... It''s great that you''ve got as far as you have - to have working models and views - but there''s some large steps you need to take that are covered well in lots of online references and in any of the books that deal with getting started with Ruby/Rails. I''d suggest visiting them and getting much more comfortable with the basics to help you to get as quickly as possible to a position where you will be able to produce useful code. -- 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.
Thanks a lot Michael.... but this (<%= User.find(:all, :select => ''name'', :conditions => ["id = ?", 1]).name %>) did not work. It gave me the following ''NoMethodError'' - undefined method `name'' for [#<User name: "Ron">]:Array :-( -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 March 2010 20:28, Rohit Shinde <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thanks a lot Michael.... but this (<%= User.find(:all, :select => > ''name'', :conditions => ["id = ?", 1]).name %>) did not work. > > It gave me the following ''NoMethodError'' - undefined method `name'' for > [#<User name: "Ron">]:ArraySorry... "find :all" returns an array... my bad. Use this instead: <%= User.find(:all, :select => ''name'', :conditions => ["id = ?", 1]).first.name %> -- 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.
That worked.... thanks a lot Michael :-) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.