SkRoR
SkRoR

Reputation: 1199

how to solve uninitialized constant error in rails

i have 3 tables categorieswith fields id,name,parent_id, coaches with id, name and join table categories_coaches with category_id and coach_id. i want join two table (categories_coaches and categories) with category_id and get the parent_id. when i run this please give me a solution for this

here is my code

Models

class Category < ActiveRecord::Base
  has_and_belongs_to_many :coaches
end

class Coaches < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

View/new.html.erb

<%= select_tag 'category', options_from_collection_for_select(@category.coaches, 'id', 'name')%>

controller

class CategoriesController < ApplicationController def new end end

when i try this iam getting this error

enter image description here

Upvotes: 0

Views: 175

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

You need to call your models by the singular name format.

You also need to make sure your associations reflect whether they are singular or plural (belongs_to is singular):

class CategoryCoach < ActiveRecord::Base
  belongs_to :category
end

This also goes for the other association:

#app/models/coach.rb
class Coach < ActiveRecord::Base
   has_many :category_coaches
end

ActiveRecord

You need to work on your ActiveRecord associations. Specifically, you've associated your models in a peculiar way.

I'd use a has_and_belongs_to_many association:

#app/models/coach.rb
class Coach < ActiveRecord::Base
   has_and_belongs_to_many :categories
end

#app/models/category.rb
class Category < ActiveRecord::Base
   has_and_belongs_to_many :coaches
end

You'd need a join table with the following:

#categories_coaches
category_id | coach_id

This will give you the ability to call @category.coaches etc:

enter image description here


Structure

I think there's a deeper structural issue to content with.

Firstly, why have you embedded your ProgrammesController in a namespace? The namespace has nothing to do with your controller structure - it just provides a way to segregate functionality in your app.

You should structure your app like this:

#config/routes.rb
resources :coaches do
    resources :programmes #-> url.com/coaches/:coach_id/programmes/new
end

#app/models/coach.rb
class Coach < ActiveRecord::Base
   has_many :programmes
end

#app/models/programme.rb
class Programme < ActiveRecord::Base
   belongs_to :coach
end

#app/controllers/programmes_controller.rb
class ProgrammesController < ApplicationController
    def new 
       @coach = Coach.find params[:coach_id]
       @programme = @coach.programmes.new
    end
    def create
       @coach = Coach.find params[:coach_id]
       @programme = @coach.programmes.new programme_params
       @programme.save
    end 

    private

    def programme_params
       params.require(:programme).permit(:x, :y, :z)
    end
end

Upvotes: 1

Max Williams
Max Williams

Reputation: 32933

Your association is mis-named:

class Category < ActiveRecord::Base
  has_many :category_coaches
end

Upvotes: 0

Related Questions