Dear eveyone,
There is a bug I can''t figure out and if someone can kindly tell me
what''s wrong.
I have 2 tables now, one is users, and another is work_times. one user has many
work_times, and each work_time belongs to one user.
The problem now is, I can see a list of
worktime--http://localhost:3000/user/list/6--here 6 is the user.id and there is
a column called user_id in work_times table as well. But instead of showing only
the work times that belongs to user_id 6, it shows everyone user_id , so I think
something is wrong below where I define @work_times=, but I''m not sure
what''s wrong there.
Code of the user_controller:
class UserController < ApplicationController
before_filter :login_required
scaffold :user
def list
current_user_id = params[:id]
@an_user = User.find(current_user_id)
current_user_id])
@work_times = WorkTime.find(:all, :condition=>["user_id=?",
current_user_id])
end
.
Code of list.rhtml: (I can see @an_user.id =6 in output)
.<title><%= @an_user.last_name + @an_user.first_name%>''s
timesheet</title>
<p>You are logged on as user: <%= @an_user.last_name +
@an_user.first_name%>
</p>
<p>user.id is <%= @an_user.id %></p>
<table border ="1">
<tr>
<th>Check in </th>
<th>Check out</th>
<th>lunch hour</th>
<th>options</td>
</tr>
<% for work_time in @work_times %>
<% time_in = work_time.check_in %>
<% time_out = work_time.check_out %>
<% lunch_time = work_time.lunch %>
<tr valign="top">
<td width="30%"><%= time_in.strftime("%y-%m-%d
%H:%M") %></td>
<td width="30%"><%= time_out.strftime("%y-%m-%d
%H:%M") %></td>
<td width="20%"><%= lunch_time.strftime("%H:%M")
%></td>
<td>
Thank you very much for any advice.
Sammy
---------------------------------
DO YOU YAHOO!?
雅虎免费G邮箱-中国第一绝无垃圾邮件骚扰超大邮箱
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
s c wrote:> Dear eveyone, > There is a bug I can''t figure out and if someone can kindly tell me > what''s wrong. > I have 2 tables now, one is users, and another is work_times. one user > has many work_times, and each work_time belongs to one user. > The problem now is, I can see a list of > worktime--http://localhost:3000/user/list/6--here 6 is the user.id and > there is a column called user_id in work_times table as well. But > instead of showing only the work times that belongs to user_id 6, it > shows everyone user_id , so I think something is wrong below where I > define @work_times=, but I''m not sure what''s wrong there. > *Code of the user_controller:* > class UserController < ApplicationController > before_filter :login_required > scaffold :user > def list > current_user_id = params[:id] > @an_user = User.find(current_user_id) > current_user_id]) > @work_times = WorkTime.find(:all, :condition=>["user_id=?", > current_user_id]) > end > .:conditions (plural)
On 8/23/05, Steve Downey <sldowney-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> wrote:> s c wrote: > > > Dear eveyone, > > There is a bug I can''t figure out and if someone can kindly tell me > > what''s wrong. > > I have 2 tables now, one is users, and another is work_times. one user > > has many work_times, and each work_time belongs to one user. > > The problem now is, I can see a list of > > worktime--http://localhost:3000/user/list/6--here 6 is the user.id and > > there is a column called user_id in work_times table as well. But > > instead of showing only the work times that belongs to user_id 6, it > > shows everyone user_id , so I think something is wrong below where I > > define @work_times=, but I''m not sure what''s wrong there. > > *Code of the user_controller:* > > class UserController < ApplicationController > > before_filter :login_required > > scaffold :user > > def list > > current_user_id = params[:id] > > @an_user = User.find(current_user_id) > > current_user_id]) > > @work_times = WorkTime.find(:all, :condition=>["user_id=?", > > current_user_id]) > > end > > . > > > :conditions (plural)or this: WorkTime.find_by_user_id(current_user_id) -- rick http://techno-weenie.net
On Tue, 2005-08-23 at 23:52 +0800, s c wrote:> Dear eveyone, > There is a bug I can''t figure out and if someone can kindly tell me > what''s wrong. > I have 2 tables now, one is users, and another is work_times. one user > has many work_times, and each work_time belongs to one user. > The problem now is, I can see a list of > worktime--http://localhost:3000/user/list/6--here 6 is the user.id and > there is a column called user_id in work_times table as well. But > instead of showing only the work times that belongs to user_id 6, it > shows everyone user_id , so I think something is wrong below where I > define @work_times=, but I''m not sure what''s wrong there. > > Code of the user_controller: > class UserController < ApplicationController > before_filter :login_required > scaffold :user > def list > current_user_id = params[:id] > @an_user = User.find(current_user_id) > current_user_id]) > @work_times = WorkTime.find(:all, :condition=>["user_id=?", current_user_id]) > endWhile it has been pointed out that condition should be plural, if your model is laid out correctly, you shouldn''t need to specify conditions. If your User model has_many :worktimes you can then do @an_user = User.find(params[:id]) @worktimes = @an_user.worktimes Or for that matter, since the worktimes become part of the user, you can just access them from the user object in the view. -- Steven Critchfield <critch-wQLwMjUOumVBDgjK7y7TUQ@public.gmane.org>