I´m having difficulties reading the content of some rows in my database.
Tried to use some of the code at page 221 in the pickaxe, but didn´t
make it work.
The SQL query works fine directly against the database.
mysql> select id, count, company from users where name = “martin”;
±—±------±-----------+
| id | count | company |
±—±------±-----------+
| 1 | 0 | |
±—±------±-----------+
def count(number)
puts "count() reached!\n user is #{@username} " # this works
userdata = User.find_by_sql("select id, count, company from users
where name = ‘@username’ ")
account = userdata[0]
puts “account-id: #{@account.id}” # empty
puts “the name of the company is: #{@account.company}, count #
{@account.count}\n” # empty
end
How do I collect the content of the id, count and company field to
variables?
end
Your last two puts lines reference @account, but the line before assigns
to account. @account will be nil unless assigned already in the current
object.
Also, the SQL sent to the database will currently contain the literal
string “@username” as the name rather than the contents of @username.
The best way to pass parameters to the database is with one of the
following two techniques:
userdata = User.find_by_sql([“select id, count, company from users where
name = ?”, @username])
userdata = User.find_by_sql([“select id, count, company from users where
name = :name”, {:name => @username})