0

I have an issue with rails orm when i tried to get a field from a "belongs_to" model.

Processing by OdooHrDepartementController#index as HTML
  Current user: admin (id=1)
  Rendered plugins/redmine_odoo_link/app/views/odoo_hr_departement/index.html.erb within layouts/base (7.6ms)
Completed 500 Internal Server Error in 140.0ms

ActionView::Template::Error (undefined method `company' for #<OdooHrDepartement:0x007f96dcdea650>):
    14:   <tr>
    15:   <td class="username"><%= dp.create_date %></td>
    16:   <td class="firstname"><%= dp.name %></td>
    17:   <td class="lastname"><%= link_to dp.company.name , { :action => "show_companies", :id => dp.company_id }%></td>
    18:   <td class="email"><%= dp.note %></td>
    19:   <td class="email"><%= dp.OdooHrDepartement_id %></td>
    20:   <td class="email"><%= dp.OdooUsers_id %></td>
  activemodel (3.2.19) lib/active_model/attribute_methods.rb:407:in `method_missing'

This is my view:

<h2>odoo departments</h2>

<table class="list">
  <thead><tr>
  <th>create date</th>
  <th>name</th>
  <th>company_id</th>
    <th>note</th>
    <th>parent_id</th>
    <th>manager_id</th>
  </tr></thead>
  <tbody>
<% for dp in @departments -%>
  <tr>
  <td class="username"><%= dp.create_date %></td>
  <td class="firstname"><%= dp.name %></td>
  <td class="lastname"><%= link_to dp.company.name , { :action => "show_companies", :id => dp.company_id }%></td>
  <td class="email"><%= dp.note %></td>
  <td class="email"><%= dp.OdooHrDepartement_id %></td>
  <td class="email"><%= dp.OdooUsers_id %></td>
  </tr>
<% end -%>
  </tbody>
</table>

This is my model:

class OdooHrDepartement < ActiveRecord::Base
  belongs_to :Company
  belongs_to :OdooHrDepartement
  belongs_to :OdooUsers 
end

This is my controller:

class OdooHrDepartementController < ApplicationController
  unloadable

  def index
    @departments = OdooHrDepartement.all
  end

  def show_companies
    @company = Company.find(params[:id])
  end
end

there are my routes:

get 'odoo_departments', :to => 'odoo_hr_departement#index'
get 'odoo_departments/:id/' , :to => 'odoo_hr_departement#show_companies'

finally this is my migration code :

class CreateOdooHrDepartements < ActiveRecord::Migration
  def change
    create_table :odoo_hr_departements do |t|
      t.timestamp :create_date
      t.string :name
      t.belongs_to :company, index: true
      t.text :note
      t.belongs_to :OdooHrDepartement
      t.belongs_to :OdooUsers
    end
  end
end

I am not sure why this doesn't work.

Can someone please help me solve the error with the undefined method company?

basiszwo
  • 575
  • 3
  • 8
H. SLF
  • 61
  • 8

2 Answers2

0

In Ruby uppercase identifiers are treated as constants. That means that you should ONLY use uppercase for constants such as class and module names and you guessed it - constants.

Other than that Ruby does not case what you name your methods, proterties and variables (or in the case of rails associations) but the convention is to always use snake_case. You are strongly recommended to follow it unless you like to look like a noob or have a very good reason not to (such as methods that mirror C functions or another external API).

class OdooHrDepartement < ActiveRecord::Base
  belongs_to :company
  belongs_to :odoo_hr_departement
  belongs_to :odoo_users 
end

Also if Odoo is the name of your company or app you might want to use a module to namespace your controllers and models instead of tacking it on the class name.

module Odoo
  class HrDepartementsController < ActiveRecord::Base

    def index
      @users = User.all # will lookup Odoo::User
    end
  end
end
max
  • 96,212
  • 14
  • 104
  • 165
0

As mentioned before you should use identifiers in downcase.

Your OdooHrDepartement should look like this

class OdooHrDepartement < ActiveRecord::Base
  belongs_to :Company
  belongs_to :OdooHrDepartement
  belongs_to :OdooUsers 
end

and your migration should also use downcase for the associations

class CreateOdooHrDepartements < ActiveRecord::Migration
  def change
    create_table :odoo_hr_departements do |t|
      t.timestamp :create_date
      t.string :name
      t.belongs_to :company, index: true
      t.text :note
      t.belongs_to :odoo_hr_departement
      t.belongs_to :odoo_users
    end
  end
end

I am not sure about whether t.belongs_to :odoo_users is correct or not because I don't know how you named your OdooUsers model.

I'd prefer using references in migrations, as e.g.

t.references :odoo_user, foreign_key: true

Same for odoo_hr_department and company.

Hope this helps.

basiszwo
  • 575
  • 3
  • 8