Use a value from a different model

Hello,
I cannot find the issue and it drives me crazy :frowning:

let's simplify. I have 2 models: saving and purchase_type
a *saving* corresponds to a *purchase_type* e.g. new sourcing or
renegotiation
a saving has exactly one purchase_type and a purchase_type has many
savings.

when I try to display the list of savings and their purchase type
(name, not Id), i get the dreaded "You have a nil object when you
didn't expect it!".

The weirdest thing is that it works for category and subcategory, but
not for purchase type.
Is this a camel casing issue? All the relevant code is below

I'm getting mad, please help :slight_smile:

Thanks,
P.

Here's the index.rhtml (extracts -- RoR tells me the issue is on the
line with *****):
  <% @saving.each do |s|%>
    <tr>
      <td><%=s.id%></td>
      <td><%=s.country.name%></td>
      <td><%=s.country_reference%></td>
      <td><%=s.supplier%></td>
      <td><%=s.buyer%></td>
      <td><%=s.category.name%></td>
      <td><%=s.subcategory.name%></td>
***** <td><%=s.purchaseType.name %></td>

the controller (saving_controller):

class SavingController < ApplicationController
  def index
    @category = Category.find(:all).collect {|c| [ c.name, c.id ] }
    @subcategory = Subcategory.find(:all).collect {|c| [ c.name,
c.id ] }
    @purchaseType = PurchaseType.find(:all).collect {|c| [ c.name,
c.id ] }

    sort_order = params[:sort_by]
    sort_order ||= 'id' # for default sort
    @saving = Saving.search(params[:search], params[:page], sort_order)
  end
end

the saving model (saving - table name = savings):

class Saving < ActiveRecord::Base
  validates_presence_of :supplier,
              :a_saving,
              :a_spend,
              :country_id,
              :category_id,
              :subcategory_id,
              :buyer,
              :purchaseType_id,

  validates_numericality_of :a_saving, :a_spend

  belongs_to :category
  belongs_to :purchaseType
  belongs_to :subcategory

  def self.search(search, page, sort_order)
    search='%' if search=='' || search=='Type your search term'
    paginate :page => page,
          :conditions => ['supplier like ?',"%#{search}%"],
          :order => sort_order
  end

end

the purchase_type model (model name = PurchaseType -table name =
purchase_types):

class PurchaseType < ActiveRecord::Base
  validates_presence_of :name
  has_many :saving
end

the controller (saving_controller):

class SavingController < ApplicationController

s/b SavingsController

class PurchaseType < ActiveRecord::Base

   has\_many :saving

s/b has_many :savings

Try changing that and let us know what happens :slight_smile:

Hi,
thanks hassan for the quick reply.
I applied your suggestions + had to change a few other things (folder
name for the views, some routes...) but that didn't help either :frowning:

any other idea?

thanks
P.

Hi,
thanks hassan for the quick reply.
I applied your suggestions + had to change a few other things (folder
name for the views, some routes...) but that didn't help either :frowning:

it should also be belongs_to :purchase_type rather than purchaseType
but I don't think that will actually make a difference (and if you
made that change you would have to rename the column to
purchase_type_id).
Have you checked that you don't simply have bad data in your database
(ie a saving with a purchaseType_id for which there is no
corresponding PurchaseType ?

Fred

Hi,
no crappy data.

I really think i have a normalization issue :frowning:
I'll try to change everything and see how it goes.

thanks 8
P.

Hi,
i finally renamed everything (removed the mixed underscores / camel
casing) and it works.

thanks,
P.