# Problem: has\_many, (belongs\_to, has\_one) creating a many\_to\_many

**URL:** <https://www.ruby-forum.com/t/problem-has-many-belongs-to-has-one-creating-a-many-to-many/146943>\
**Category:** Rails\
**Created:** [August 27, 2008, 12:07am UTC](https://www.ruby-forum.com/t/problem-has-many-belongs-to-has-one-creating-a-many-to-many/146943 "2008-08-27T00:07:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kmiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3ec8ea/32.png) [@kmiller](https://www.ruby-forum.com/u/kmiller)\
**Post date:** [August 27, 2008, 12:07am UTC](https://www.ruby-forum.com/t/problem-has-many-belongs-to-has-one-creating-a-many-to-many/146943/1 "2008-08-27T00:07:25Z")

</div>

I’m very much a rookie with Rails so feel free to clue me in 🙂

I have two tables: Series, Paintings

I’m trying to set this one\_to\_many up so that I have many paintings  
within a series i.e

Series: italy  
Paintings: pic1, pic2, pic3, etc…

When rails saves the painting and series, it repeats the series for  
each painting:

# Table Dump

(dev) kmiller /home/kmiller/workspace/test:echo ‘select \* from series’  
| $MYSQL -Dtest\_development -t  
±—±------+  
| id | desc |  
±—±------+  
| 1 | italy |  
| 2 | italy |  
| 3 | italy |  
±—±------+  
(dev) kmiller /home/kmiller/workspace/test:echo ‘select \* from  
paintings’ | $MYSQL -Dtest\_development -t  
±—±----------±----------±-------------±----------------------  
±----------±-------±------±-------+  
| id | parent\_id | series\_id | content\_type | filename |  
thumbnail | size | width | height |  
±—±----------±----------±-------------±----------------------  
±----------±-------±------±-------+  
| 1 | NULL | 1 | image/jpeg | 31032008010.jpg |  
NULL | 124730 | 500 | 375 |  
| 2 | 1 | NULL | image/jpeg | 31032008010\_thumb.jpg |  
thumb | 2743 | 50 | 38 |  
| 3 | NULL | 2 | image/jpeg | 31032008012.jpg |  
NULL | 128985 | 500 | 375 |  
| 4 | 3 | NULL | image/jpeg | 31032008012\_thumb.jpg |  
thumb | 3090 | 50 | 38 |  
| 5 | NULL | 3 | image/jpeg | 31032008011.jpg |  
NULL | 131269 | 500 | 375 |  
| 6 | 5 | NULL | image/jpeg | 31032008011\_thumb.jpg |  
thumb | 2878 | 50 | 38 |  
±—±----------±----------±-------------±----------------------  
±----------±-------±------±-------+

Shouldn’t the relevant painting rows all point to a single, unique  
series row? At least that’s what I wanted to happen. I also tried  
belongs\_to in the Painting table but got the same result.

Here’s the code:

# Models:

class Series \< ActiveRecord::Base  
has\_many :paintings  
end

class Painting \< ActiveRecord::Base  
has\_one :series, :foreign\_key =\> ‘series\_id’

has\_attachment :content\_type =\> :image,  
:storage =\> :file\_system,  
:max\_size =\> 10.megabytes,  
:resize\_to =\> ‘500x500\>’,  
:thumbnails =\> { :thumb =\> ‘50x50\>’ },  
:storage =\> :file\_system,  
:path\_prefix =\> ‘/public/uploads’

validates\_as\_attachment

end

ActiveRecord::Schema.define(:version =\> 2) do

create\_table “paintings”, :force =\> true do |t|  
t.integer “parent\_id”  
t.integer “series\_id”  
t.string “content\_type”  
t.string “filename”  
t.string “thumbnail”  
t.integer “size”  
t.integer “width”  
t.integer “height”  
end

create\_table “series”, :force =\> true do |t|  
t.string “desc”  
end  
end

# Controller:

class PaintingsController \< ApplicationController  
def index  
@paintings = Painting.find(:all, :conditions =\> { :thumbnail =\>  
nil })  
end

def new  
@series = Series.new  
@painting = Painting.new  
end

def create  
@series = Series.new(params[:series])  
@painting = @series.paintings.build(params[:painting])  
sleep 4  
if @series.save  
flash[:notice] = “Added painting”  
redirect\_to paintings\_path  
else  
render :action =\> ‘new’  
end  
end  
end

# View

\<% form\_for(:painting, :url =\> { :action =\> “create” }, :html =\>  
{ :multipart =\> true }) do |f| %\>

series:  
\<%= text\_field :series, :desc %\>

image:  
\<%= f.file\_field :uploaded\_data %\>

 \<%= submit\_tag 'Create' %\> \<% end -%\>

Appreciate any help! - thanks

---

<div class="post-metadata">

**Author:** ![kmiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3ec8ea/32.png) [@kmiller](https://www.ruby-forum.com/u/kmiller)\
**Post date:** [August 27, 2008, 12:14am UTC](https://www.ruby-forum.com/t/problem-has-many-belongs-to-has-one-creating-a-many-to-many/146943/2 "2008-08-27T00:14:27Z")

</div>

On Aug 26, 9:39 pm, KMiller [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Shouldn’t the relevant painting rows all point to a single, unique  
> series row? At least that’s what I wanted to happen. I also tried  
> belongs\_to in the Painting table but got the same result.

2 things:  
it should be a belongs\_to. Painting has a series\_id column so it has  
to be Painting belongs\_to :series.  
Secondly, the reason that a new instance of Series is created each  
time is because you’re asking for one. If you want the painting to be  
appended to a given Series then you should be using Series.find … to  
retrieve the appropriate Series, rather than creating a new one with  
Series.new

Fred
