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:
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.