Problem: has_many, (belongs_to, has_one) creating a many_to_many

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

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

On Aug 26, 9:39 pm, KMiller [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs