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 ?
On 19 Nov 2007, at 14:45, Chewbacca wrote:
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
On Nov 19, 6:45 am, Chewbacca [email protected] wrote:
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 ?
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
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!
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.
should work
have a look in development.log,
copy the generated sql an run it on your database
see, if you get results
by the way:
why do you user userid, not user_id ?
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 ?
On 19 Nov, 19:38, Thorsten M. [email protected]
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”}
[4;36;1mSQL (0.000000) [0m [0;1mSET NAMES ‘UTF8’ [0m
[4;35;1mUser Columns (0.001000) [0m [0mSHOW FIELDS FROM
users [0m
[4;36;1mUser Load (0.001000) [0m [0;1mSELECT * FROM users WHERE
(users.id
= 1) LIMIT 1 [0m
[4;35;1mPass Columns (0.001000) [0m [0mSHOW FIELDS FROM
passes [0m
[4;36;1mPass Load (0.001000) [0m [0;1mSELECT * FROM passes WHERE
(passes.user_id
IS NULL) [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 ?
On 19 Nov, 20:30, Thorsten M. [email protected]
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.
Chewbacca wrote:
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 ?
On 19 Nov, 19:38, Thorsten M. [email protected]
ok, one step
so there is nothing in params[:userid]
that depends on the calling page
in development.log you can see all the params, that are handed over to
your controller. or does it fail after your redirect from the new
action?
but since the new action has @current_user, you could use @current_user
in index, too? maybe the user_id of the current user is stored in the
session? this would
make sense, since it would be a bad habbit anyway, to use a user_id,
that’s part of the html-view, since everybody can change it there and
become whoever he wants to.
Thanks Thorsten for your explanations. I have downloaded the book and
will study it closely.
On 20 Nov, 11:15, Thorsten M. [email protected]
@pass = Pass.find_all_by_user_id(session[:user_id])
ok, the main problem here was, that you tried to user params[:user_id]
instead of session[:user_id] (and userid instead of user_id)
params is filled by the stuff the was send by the browser with the url
eg:
http://localhost:3000/categories/3/articles?page_number=0
would give you a params hash containing
params[:category_id] = 3
params[:page_number] = 0
plus an :action and and :controller. all that is filled out by rails, so
categories/3 becomes category_id. that’s why it’s so important to stick
with rails defaults for naming. (you can tell rails individual
fieldnames if you must, but avoid it)
the session which you use to store the user_id has nothing to do with
this. it’s on the serverside and identified by a kind of unique id. it’s
too much to go into details here, but it’s explained here
http://www.sitepoint.com/books/rails1/freebook.php
the find you use is ok so far, you could have done this in different
ways.
maybe the most rails like:
@pass = @current_user.passes
for this your User model would need a
has_many :passes
and the Pass model
belongs_to :user
in the freebook.php you will find those things explained