I'm using rails 3.2.14 and having trouble using has\_many :through for the first time. I'm using a has\_many :through relationship between Image and Design tables using a Design\_Pictures table that will store the order ranking for design images. All Images are associated with one User. Later I want the flexibility to store other images in the Images table that are not associated with a particular design. 

I can successfully add sample data to my database and show the image title (which is stored in the images table) and ranking (which is stored in the design\_pictures table) in my show design pages. What I can't figure out is how to create a new design\_picture. Once I can get this working I'm going to use CarrierWave or Paperclip to add images to the Image table.

Here are my models:

**app/models/image.rb:**

```
class Image < ActiveRecord::Base
  attr_accessible :title
  belongs_to :user
  has_many :design_pictures, dependent: :destroy 
  has_many :designs, :through => :design_pictures

  validates :user_id, presence: true
  validates :title, length: { maximum: 80 }
end

```

**app/models/design.rb:**

```
class Design < ActiveRecord::Base
  attr_accessible :title
  has_many :design_pictures, dependent: :destroy 
  has_many :images, :through => :design_pictures

  validates :title, presence: true, length: { maximum: 60 }
end

```

**app/models/design\_picture.rb:**

```
class DesignPicture < ActiveRecord::Base
  attr_accessible :ranking
  belongs_to :image
  belongs_to :design

  validates :image_id, presence: true
  validates :design_id, presence: true   

  default_scope order: 'design_pictures.ranking ASC'
end

```

**app/models/user.rb:**

```
class User < ActiveRecord::Base
  attr_accessible :name
  has_many :designs, dependent: :destroy 
  has_many :images, dependent: :destroy

  validates :name, presence: true, length: { maximum: 50 }
end

```

Views:

**app/views/designs/show.html.erb:**

```
            <div>
                <% if @design.design_pictures.any? %>
                    <h3>Images (<%= @design.design_pictures.count %>)</h3>
                    <ul>
                        <%= render @design_pictures %>
                    </ul>
                <% end %>
            </div>

            <div>
                <%= render 'shared/design_picture_form' %>
            </div>

```

**app/views/design\_pictures/\_design\_picture.html.erb**

```
<li>
    <%= design_picture.image.title %> - Ranking: <%= design_picture.ranking %>
</li>

```

**app/views/shared/\_design\_picture\_form.html.erb:**

```
<%= form_for(@design_picture) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>

    <%= f.label :title, "Title" %>
    <%= f.text_field :title, 
                placeholder: "Name your image.",
                class: "" %>

    <%= f.label :ranking, "Ranking" %>
    <%= f.text_field :ranking, 
            placeholder: "Rank your design picture.",
            class: "" %>            

    <%= hidden_field_tag(:design_id, @design.id) %>

    <%= f.submit "Add Image", class: "btn btn-large btn-primary" %>
<% end %>

```

Controllers:

**app/controllers/designs\_controller.rb**

```
    def show
        @design = Design.find(params[:id])
        @design_pictures = @design.design_pictures.find(:all, :limit => 10)

        @image = current_user.images.build 
        @design_picture = @design.design_pictures.build
    end

```

**app/controllers/design\_pictures\_controller.rb**

```
    def create
        @current_design = Design.find(params[:design_id])
        @image = current_user.images.new(params[:title])
        @design_picture = @current_design.design_pictures.build(:image_id => @image.id, params[:ranking])

        if @design_picture.save
            flash[:success] = "Micropost created!"
            redirect_to @current_design
        else
            redirect_to designs_url
        end
    end

```

Error when I visit the design\_picture form partial:

```
undefined method `title' for #<DesignPicture image_id: nil, design_id: 1427, ranking: nil>

```

If I remove the title from the form I can submit form but nothing is created. 

Any help would be greatly appreciated.