# Trying to connect tables

**URL:** <https://www.ruby-forum.com/t/trying-to-connect-tables/77001>\
**Category:** Rails\
**Created:** [March 5, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/trying-to-connect-tables/77001 "2006-03-05T20:39:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cranberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@cranberry](https://www.ruby-forum.com/u/cranberry)\
**Post date:** [March 5, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/trying-to-connect-tables/77001/1 "2006-03-05T20:39:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cranberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@cranberry](https://www.ruby-forum.com/u/cranberry)\
**Post date:** [March 5, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/trying-to-connect-tables/77001/2 "2006-03-05T20:50:27Z")

</div>

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](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![cranberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@cranberry](https://www.ruby-forum.com/u/cranberry)\
**Post date:** [March 5, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/trying-to-connect-tables/77001/3 "2006-03-05T20:57:03Z")

</div>

> 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](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?

---

<div class="post-metadata">

**Author:** ![cranberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@cranberry](https://www.ruby-forum.com/u/cranberry)\
**Post date:** [March 5, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/trying-to-connect-tables/77001/4 "2006-03-05T21:01:27Z")

</div>

> 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](http://blog.hasmanythrough.com)
