Question

I'm trying tu use Single Table inheritance to represent that employees can be managers or consultants. So here's my employees model:

class Employee < ActiveRecord::Base
  belongs_to :communaute
      self.inheritance_column = :fonction

  scope :manager, -> { where(fonction: 'Manager') } 
  scope :consultant, -> { where(fonction: 'Consultant') } 
end

and here are my subclasses :

class Consultant < Employee
end


class Manager < Employee
end

When i'm oppening the new view to create an employee. I have undefined method `fonction' error when i'm trying to list the types on employees. I don't understand why because i defined it in the employee controller. Could you please help me on this error please.

Below my new form

<%= form_for(@employee) do |f| %>
  <% if @employee.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@employee.errors.count, "error") %> prohibited this employee from being saved:</h2>

      <ul>
      <% @employee.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>

  <div class="field">
  <%= f.label :fonction %><br>
  <%= f.select :fonction, Employee.fonction.map {|r| [r.humanize, r.camelcase]}, {}, disabled: @fonction !=    "Employee" %> 

  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And my employee controller

class EmployeesController < ApplicationController
  before_action :set_employee, only: [:show, :edit, :update, :destroy]
  before_action :set_fonction

  def index
    @employees = Employee.all
    #@employees = fonction_class.all
  end

  def show
  end

  def new
    #@employee = fonction_class.new
    @employee = Employee.new
  end

  def edit
  end

  def create 
       @employee = Employee.new(employee_params)
       if @employee.save
         redirect_to @employee, notice: "#{fonction} was successfully created."
       else
         render action: 'new'
       end
   end

  def update
  end

  def destroy
  end

  private
  def fonction 
      params[:type] || "Employee" 
  end
     def set_fonction
        @fonction = fonction 
     end
    # def fonction_class 
     #    fonction.constantize 
     #end

     def set_animal
         @employee = fonction_class.find(params[:id])
       end
       def employee_params 
            params.require(fonction.downcase.to_sym).permit(:name, :fonction) 
        end
end
Was it helpful?

Solution

You need to define fonction in your model (app/models/employee.rb)

The way you're using it implies it's defined as both a class method, and an instance method. It also looks like it could be an array? Employee.fonction.map {|r| ... or an attribute (since you have a select to set it on an employee.)

However, you define fonction as a string...

def fonction 
  params[:type] || "Employee" 
end

So I'm not really sure what you're trying to achieve here... All I know is Employee.fonction looks like a model class method...

def self.fonction
  ...
end

and f.select :fonction looks like an instance method or attribute?

EDIT I think I've got this... (Sorry, I missed fonction reference in your model definition)

Try adding this to your Employee model:

def self.fonction
  ["Manager", "Consultant", "Employee"]
end

EDIT #2

You can also define a constant in your model

FONCTIONS = ["Manager", "Consultant", "Employee"]

def fonction
  FONCTIONS
end

OTHER TIPS

Did you create fonction column?, create it by entering in your terminal, when in your app directory:

rails g migration add_fonction_to_employees fonction
rake db:migrate

Then check your DB has the column fonction in employees table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top