Newb question- method not working

I know this shouldn’t be in the view but to test the code it is.
Anyway if I do just session[user] I get back the user id (the id from
the user table)
which is good.
However this code does not work.

<% a = session[:user] %>
<% uname = User.find(:first, :conditions => id = a) %>
<%= @uname %>

TIA
Stuart

On 8/8/06, Dark A. [email protected] wrote:

TIA
Stuart


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

try this

<% uname = User.find(:first, :conditions => [“id = ?”, a] ) %>

lemme know if it doesn’t work
-daya

Stuart Fellowes wrote:

I know this shouldn’t be in the view but to test the code it is.
Anyway if I do just session[user] I get back the user id (the id from
the user table)
which is good.
However this code does not work.

<% a = session[:user] %>
<% uname = User.find(:first, :conditions => id = a) %>
<%= @uname %>

TIA
Stuart

The first reply is indeed correct. That is not valid ruby syntax.

remember that anytime you see a => you are dealing with a hash. Hashes
have a key and a value. So what is the value of the :conditions key in
your hash? a string? another hash? It’s very unclear.

So the => must point to an object of some sort.

:conditions => “id = #{a}”

Would work, where #{a} is swapped out for whatever the value of a is.
However, for security reasons ActiveRecord support a better way.

:conditions => [“id = ?”, a]

Now the value of :conditions is an array, with the SQL fragment first,
and then a dynamic value. That value is inserted in the ? spot and
properly quoted so that noone can use SQL injection attacks on your
code.

Also, if you are running edge rails you can use a hash as the value.

:conditions => {:id => a}

Last, but not least, rails has a shortcut dynamic method to find a
record where an attribute equals something.

User.find_by_id(a)
User.find_all_by_status(“admin”)

A long answer to a simple question I suppose, but I hope it helps you
understand a little more about awesome ActiveRecord is.

On Tue, 2006-08-08 at 12:13 -0500, linux user wrote:

    <% a = session[:user] %> 
    <% uname = User.find(:first, :conditions => id = a) %>
    <%= @uname %>

Do you want to assign a to id, or do you want to test equality of id and
a?

bye
John

Try
<%= User.find(session[:user].id).name %>

(assuming user has a name attribute).

Hope this helps,
Zack

Hi Stuart,

try User.find(session[:user]) instead. Rails is often that simple.

The way of doing things in a condition would be User.find(:first,
:condition
=> [“name like ?”, name]) but this isn’t necessary for a simple
id-search.

Cheers,
Jan

On 8/8/06, John A. [email protected] wrote:

On Tue, 2006-08-08 at 12:13 -0500, linux user wrote:

Do you want to assign a to id, or do you want to test equality of id and
a?

bye
John

I want a assigned to id.

Stuart

Wow …weird problem , I did the simple id search and get back:
Couldn’t find User with ID=29761836
Which would be correct but I guess the problem is somewhere else.

Stuart

Maybe this:
<% a = session[:user] %>
<% uname = User.find(:first, :conditions => “id = ‘#{a}’”) %>
<%= @uname %>

Att.,
Romeu Fonseca
-----Mensagem original-----
De: [email protected]
[mailto:[email protected]] Em nome de Zack C.
Enviada em: terça-feira, 8 de agosto de 2006 12:54
Para: [email protected]
Assunto: Re: [Rails] Newb question- method not working

Try
<%= User.find(session[:user].id).name %>

(assuming user has a name attribute).

Hope this helps,
Zack

On 8/8/06, Dark A. [email protected] wrote:

TIA
Stuart


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.10.7/411 - Release Date: 7/8/2006


No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.10.7/411 - Release Date: 7/8/2006

Nope neither of these are working. Weird.
With Zack’s I keep getting this exception thrown:
Couldn’t find User with ID=59523673

Yet with debug(session) it’s showing a differend :user .

— !ruby/object:CGI::Session
data: &id001
:user: 29761836
flash: !map:ActionController::Flash::FlashHash {}

bman: &id002 !ruby/object:CGI::Session::ActiveRecordStore
session: !ruby/object:CGI::Session::ActiveRecordStore::Session
attributes:
updated_at: 2006-08-08 14:15:44
session_id: be9f39bbe191dcf1b1c2f1b111b44c4d
id: “4”
data: |
BAh7BzoJdXNlcmkELCHGASIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6
Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA==

data: *id001

dbprot:

  • *id002
    new_session: false
    session_id: be9f39bbe191dcf1b1c2f1b111b44c4d

<%= @uname = User.find_by_id(session[:user]) %>
or
<%= @uname = User.find_by_id(session[:user].id) %>

This will be cleaner… remember that find_by_XX is the same as
find(:first, :conditions => [“XX = ?”, XX]) or find_all_by_XX returns
:all

You can check the section “Dynamic attribute-based finders” in
http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000858

Romeu Henrique Capparelli F. wrote:

Maybe this:
<% a = session[:user] %>
<% uname = User.find(:first, :conditions => “id = ‘#{a}’”) %>
<%= @uname %>

Hi Dark,

Zacks solution (as well as mine) is assuming that what you get with
session[:user] is indeed an ID that is corresponding to the AR-ID of an
User-Object. If this isn’t true it’s not working of course.

Regarding your debug output I assume that you wan’t to get the id=4 and
find
this one on the users table. So you need to find out how to get this id.
I
don’t know what you are using to write the user to the session but you
might
try (and error out) session[:user][:id] or session[:id] or
session[:user] or
session[:user].id or session[‘user’] and so on.

Sometimes you’ve got to do some trial and error. What’s the readme of
your
authorization plugin telling?

Cheers,
Jan