I have updated my user migration changing the column “user” with “username” and now when running
User.find_by_id(session[:user_id])
server outputs
SQLite3::SQLException: no such column: user: SELECT "users".* FROM "users" WHERE "users"."id" IS NULL ORDER BY user LIMIT 1
Why it still trying ordering by “user” ??
thanks.
UPDATE:
here’s user table
create_table "users", :force => true do |t|
t.string "username", :null => false
t.string "avatar"
t.string "hashed_password", :null => false
t.string "salt", :null => false
t.string "mail", :null => false
t.integer "login_count", :default => 0
t.datetime "last_login", :default => '2011-06-23 08:59:41'
t.datetime "last_request_at", :default => '2011-06-23 08:59:41'
t.string "user_type", :default => "ruolo da definire"
t.integer "ammonizioni", :default => 0
t.integer "numero_segnalazioni_accettate", :default => 0
t.integer "numero_segnalazioni_risolte", :default => 0
t.datetime "created_at"
t.datetime "updated_at"
UPDATE 2: if it helps … I’m using cancan and the error come from the method “current_user” on application controller:
def current_user
@current_user = User.find_by_id(session[:user_id])
end
The solution of the problem has reveaved a new one:
def User.authenticate(user, password)
if user = find_by_username(user)
if user.hashed_password == encrypt_password(password, user.salt)
user
end
end
end
This worked well until I changed the migration. THe error for the first problem is repeated:
SQLite3::SQLException: no such column: user: SELECT "users".* FROM "users" WHERE "users"."username" IS NULL ORDER BY user LIMIT 1
How should I modify this?
if user = User.find_by_username(user)
Answering to Mike:
- yes
session[:user_id]is null because I’m not logged in - your solution, for the second problem, produce the same error!
This should work
or alternatively,
The important point to note is that I’m checking to see that
session[:user_id]is not null. If you look at your SQL error...WHERE "users"."id" IS NULL ...it is indeed null.Update
That’s why I ended with
if session[:user_id]to ensure such a session exists in the first place.Update #2
To prove that
.find_by_username(user.username)works and simply passing inuserI just tried a testOf course, as expected,
User.find_by_name(User.all.last.name)works 🙂