find_by problems

I am a newbie in Rails and I am just trying to learn. I have this list
that lists all records from a table:

@pass = Pass.find :all

Now, I want to list only the records that belongs to the specific user
that is logged on.
The table passes contain a column named 'userid', which is
automatically entered when the user post a message in the passes
table.

I try with:

@pass = Pass.find_all_by_userid(params[userid])

but this will only render an empty list.

Suggestions anyone ? What am I doing wrong ?

I am a newbie in Rails and I am just trying to learn. I have this list
that lists all records from a table:

@pass = Pass.find :all

Now, I want to list only the records that belongs to the specific user
that is logged on.
The table passes contain a column named 'userid', which is
automatically entered when the user post a message in the passes
table.

I try with:

@pass = Pass.find_all_by_userid(params[userid])

This could just be a typo in your email, but that should be

@pass = Pass.find_all_by_userid(params[:userid])

Fred

Nope, no typo in the mail, but a typo in my code. I will try this when
I get home in a couple of hours. Thanks Fred!

you'll see this hash with indifferent access a lot. Once in a while,
you have to use the symbol or string form of the key (tho i can't
remember any specific cases right now)

http://api.rubyonrails.org/classes/HashWithIndifferentAccess.html

I tested with the suggestion from Fred, but that did not help. The
list is still empty.
It works fine to hard-code the userid, but then the same records are
being shown every time.

This is what my controller looks like right now, and it produces a
blank list:

class PassController < ApplicationController
before_filter :login_required

  def index
    @pass = Pass.find(:all, :conditions =>
["userid=?",params[:userid]])
  end

  def new
    @pass=Pass.new(params[:pass])
    @pass.userid = @current_user
    if request.post? and @pass.save
      flash[:notice] = 'Träningspasset sparades'
      redirect_to :action => 'index'
    end
  end
end

I would like to use the dynamic find_by if it is possible.

I did call it user_id from the beginning, but changed it during my
fiddling with the code. I have now changed it back.

The generated SQL says:
SELECT * FROM users WHERE (users.`id` = 1) LIMIT 1
SELECT * FROM passes WHERE (user_id=NULL)

I understand as much as I do not have a user_id yet... How do I
retrieve that ?

you can scope the find by user id as follows:

@pass = current_user.pass.find :all

Here is the development.log:

Processing PassController#index (for 127.0.0.1 at 2007-11-19 21:02:10)
[GET]
  Session ID: a39749a398f7c8f8d544bc9c324369e0
  Parameters: {"action"=>"index", "controller"=>"pass"}
  e[4;36;1mSQL (0.000000)e[0m e[0;1mSET NAMES 'UTF8'e[0m
  e[4;35;1mUser Columns (0.001000)e[0m e[0mSHOW FIELDS FROM
userse[0m
  e[4;36;1mUser Load (0.001000)e[0m e[0;1mSELECT * FROM users WHERE
(users.`id` = 1) LIMIT 1e[0m
  e[4;35;1mPass Columns (0.001000)e[0m e[0mSHOW FIELDS FROM
passese[0m
  e[4;36;1mPass Load (0.001000)e[0m e[0;1mSELECT * FROM passes WHERE
(passes.`user_id` IS NULL) e[0m
Rendering within layouts/application
Rendering pass/index
Completed in 0.09800 (10 reqs/sec) | Rendering: 0.00400 (4%) | DB:
0.00400 (4%) | 200 OK [http://localhost/pass]

A small explanation on the application.
It is a training dairy, where players write down their training
sessions (passes). When they list the passes, I want the list to only
show their own passes and not other players training passes. So the
table 'passes' have a column called user_id, which is automatically
entered whenever the user saves a new training session. The table
'users' have of course the _id column.

Thorsten, your suggestion about using @current_user is interesting,
and I would like to pursue that. However I am just a beginner and do
not fully understand what you meant. Can you explain some more ?

I solved it!

@pass = Pass.find_all_by_user_id(session[:user_id])

Unfortunately I do not really understand what I did right :o)

If anyone can explain what the above line means, it would be deeply
appreciated.