0

Hi I'm currently trying to build a photo album with many users attached to it, and I'm trying to fix the relationship between them so that it's a many to many (one user + multiple albums, one album + multiple users). However I after touching the model and migrations I am receiving an error when I try to load my show.html.erb. Please help!!

error:

uninitialized constant User::UserAlbum

show.html.erb

<% provide(:title, "Welcome, #{@user.name}!") %>

<div>
    You currently have <%= pluralize(@user.albums.count, "album") %>
</div>

<div>
    <%= link_to "Create a new album!", new_user_album_path(@user) %>
</div>

<div>
<% if @user.albums.any? %>
hey
<% else %>
boo
<% end %> 
</div>

<%= link_to "Back", users_path %>

album model

class Album < ActiveRecord::Base
  attr_accessible :avatar, :name, :description
  has_many :user_albums
  has_many :users, :through => :user_albums
  has_many :photos
end

user model

class User < ActiveRecord::Base

  has_secure_password
  attr_accessible :email, :name, :password, :password_confirmation
  validates_presence_of :password, :on => :create

  validates_format_of :name, :with => /[A-Za-z]+/, :on => :create
  validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
  validates_length_of :password, :minimum => 5, :on => :create

  has_many :user_albums
  has_many :albums, :through => :user_albums
  accepts_nested_attributes_for :albums

end

photo model (maybe not necessary to look at this)

class Photo < ActiveRecord::Base
  belongs_to :album
end

user_albums model

class UserAlbums < ActiveRecord::Base
 belongs_to :album
 belongs_to :user
end

schema

ActiveRecord::Schema.define(:version => 20121001160745) do

create_table "albums", :force => true do |t|
  t.string   "name"
  t.datetime "created_at",  :null => false
  t.datetime "updated_at",  :null => false
  t.string   "description"
end

create_table "photos", :force => true do |t|
  t.string   "avatar_file_name"
  t.string   "avatar_content_type"
  t.integer  "avatar_file_size"
  t.datetime "avatar_updated_at"
  t.datetime "created_at",          :null => false
  t.datetime "updated_at",          :null => false
  t.integer  "album_id"
end

create_table "user_albums", :force => true do |t|
  t.datetime "created_at", :null => false
  t.datetime "updated_at", :null => false
  t.integer  "user_id"
  t.integer  "album_id"
end

create_table "users", :force => true do |t|
  t.string   "name"
  t.string   "email"
  t.datetime "created_at",      :null => false
  t.datetime "updated_at",      :null => false
  t.string   "password_digest"
end

end

user controller

class UsersController < ApplicationController

    def index
      @users = User.all

      respond_to do |format|
        format.html
        format.json { render json: @users }
      end
    end

    def new
      @user = User.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @user }
      end
    end 

    def create
      @user = User.new(params[:user])

      respond_to do |format|
        if @user.save
          format.html { redirect_to @user, notice: 'User successfully created.' }
          format.json { render json: @user, status: :created, location: @user }
        else
          format.html { render action: 'new' }
          format.json { render json: @user.errors, status: :unprocessable_entity }
        end
      end
    end

    def show
      @user = User.find(params[:id])
    end

    def update
      @user = User.update_attributes(params[:user])
      if @user.save
        format.html { redirect_to @user, notice: 'User successfully updated.'}
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: 'edit' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end

end

SOLUTION: My UserAlbums model was plural instead of UserAlbum.

bigpotato
  • 26,262
  • 56
  • 178
  • 334

1 Answers1

0

Do you have a UserAlbum model?

If not - you'll need to create one for your associations to be valid.

class UserAlbum < ActiveReocrd::Base
  belongs_to :user
  belongs_to :album
end
tamersalama
  • 4,093
  • 1
  • 32
  • 35