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 -- Posted via http://www.ruby-forum.com/.
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 > > endIt''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 -- Posted via http://www.ruby-forum.com/.
> 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.comJosh, thank you so much. I made the changes you suggested, however, I am still getting the error. What else could be the problem? -- Posted via http://www.ruby-forum.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?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 -- Posted via http://www.ruby-forum.com/.