# Activerecord problem with contrains,belongs\_to and has\_many keyword

**URL:** <https://www.ruby-forum.com/t/activerecord-problem-with-contrains-belongs-to-and-has-many-keyword/116561>\
**Category:** Rails\
**Created:** [October 7, 2007, 10:53am UTC](https://www.ruby-forum.com/t/activerecord-problem-with-contrains-belongs-to-and-has-many-keyword/116561 "2007-10-07T10:53:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghostrifle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f04885/32.png) [@ghostrifle](https://www.ruby-forum.com/u/ghostrifle)\
**Post date:** [October 7, 2007, 10:53am UTC](https://www.ruby-forum.com/t/activerecord-problem-with-contrains-belongs-to-and-has-many-keyword/116561/1 "2007-10-07T10:53:00Z")

</div>

Hi,

I have two models:

AdvertisementImages (table: advertisement\_images)  
Advertisement (table: advertisements)

The table advertisement\_images has a foreign key to advertisements  
called advertisement\_id. Both tables has one entry where the entry in  
dvertisement\_images belongs to advertisement.

The model code looks lik this so far:

class AdvertisementImages \< ActiveRecord::Base  
file\_column :image  
belongs\_to :advertisement  
end

class Advertisement \< ActiveRecord::Base  
has\_many :advertisementimage  
validates\_presence\_of :title, :body  
end

Well… I want now have access to the advertisement images where ever  
I display an advertisement.

for example:

I have a view where I can edit an advertisment. In this view I wanna  
display the images as well… My problem is, that the advertisement  
doesn’t fetch the images automatically.

When I dump the advertisement via \<%= debug @advertisement %\>, my  
output is :

— &id001 !ruby/object:Advertisement  
attributes:  
valid\_to: “2007-10-06”  
body: some text  
title: Villa Katherina  
service:  
id: “1”  
valid\_from: “2007-10-06”  
active: “0”  
parent:  
errors: !ruby/object:ActiveRecord::Errors  
base: \*id001  
errors: {}

I must have done a mistake somewhere in my declarations or so… does  
anyone know what I’ve done wrong ??

Thanx in advance,

alex !

---

<div class="post-metadata">

**Author:** ![ghostrifle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f04885/32.png) [@ghostrifle](https://www.ruby-forum.com/u/ghostrifle)\
**Post date:** [October 7, 2007, 11:55am UTC](https://www.ruby-forum.com/t/activerecord-problem-with-contrains-belongs-to-and-has-many-keyword/116561/2 "2007-10-07T11:55:13Z")

</div>

Hi,

well I found my error.

I named the image model AdvertisementImages instead of  
AdvertisementImage.

Changing the name solved my problem.

Thanx for the guy in the irc channel for helping me out !

Bye, Alex
