Hello, I cannot find the issue and it drives me crazy
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
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