Forum: Ruby on Rails Trying to connect tables .....

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
cranberry (Guest)
on 2006-03-05 21:39
I am trying to 'connect' two tables AdHour and Ad in the method below.
However, the line with the asterisks is causing the following error:

"You have a nil object when you didn't expect it!
The error occured while evaluating nil.user_id"

I've included the model for both AdHour and Ad.

Why doesn't the script like how I am connecting the tables??

Thank you!

----------------------------------------------------------------


def edit
        userid = @session[:user].id

        @ad_hour = AdHour.find(@params[:id])

 *****  if (@ad_hour.Ad.user_id != userid)*****
        render_text "You do not have permission to modify this ad." and
return
        end

end

class AdHour < ActiveRecord::Base
        belongs_to :ad
end

class Ad < ActiveRecord::Base
       has_one     :ad_hour
end
Josh S. (Guest)
on 2006-03-05 21:50
cranberry wrote:
> def edit
>         userid = @session[:user].id
>
>         @ad_hour = AdHour.find(@params[:id])
>
>  *****  if (@ad_hour.Ad.user_id != userid)*****
>         render_text "You do not have permission to modify this ad." and
> return
>         end
>
> end

It's almost always better to store the id in the session, not the user
object.  Also, use of the instance variable style access to session,
params, etc. is deprecated, so just use the accessors. But the main
error is that you used "Ad" instead of "ad" for the accessor. "Ad" is
the class name, "ad" is the accessor. Try this:

def edit
  @ad_hour = AdHour.find(params[:id])
  if (@ad_hour.ad.user_id != session[:user_id])
    render_text "You do not have permission to modify this ad."
    return
  end
  #...
end

--josh
http://blog.hasmanythrough.com
cranberry (Guest)
on 2006-03-05 21:57
> It's almost always better to store the id in the session, not the user
> object.  Also, use of the instance variable style access to session,
> params, etc. is deprecated, so just use the accessors. But the main
> error is that you used "Ad" instead of "ad" for the accessor. "Ad" is
> the class name, "ad" is the accessor. Try this:
>
> def edit
>   @ad_hour = AdHour.find(params[:id])
>   if (@ad_hour.ad.user_id != session[:user_id])
>     render_text "You do not have permission to modify this ad."
>     return
>   end
>   #...
> end
>
> --josh
> http://blog.hasmanythrough.com

Josh, thank you so much. I made the changes you suggested, however, I am
still getting the error. What else could be the problem?
Josh S. (Guest)
on 2006-03-05 22:01
> Josh, thank you so much. I made the changes you suggested, however, I am
> still getting the error. What else could be the problem?

I'd guess that it's the data itself. Take a look at the data in the
database and make sure there is an ad_hour record with a ad_hour_id with
the right value in it.

--josh
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.