I am trying to create a custom response to a particular action being
called in my controller and I am getting some really weird errors. I
am running the rails 1.2 stable branch.
Here is the action in my controller:
GET /:year/:month/level/1
GET /:year/:month/level/1.xml
def show
@level = Level.find(params[:id])
@assets = @level.assests.find_by_year_month(params[:year],
params[:month])
respond_to do |format|
format.html # show.rhtml
format.xml { @level.to_xml :include => [ @assests ] }
end
end
Here are the models:
class Level < ActiveRecord::Base
has_many :assets
end
class Asset < ActiveRecord::Base
belongs_to :level
def self.find_by_year_month(year, month)
requested_date = Date.new(year.to_i, month.to_i, 1)
from = requested_date - 1
to = requested_date >> 1
find_with_deleted(:all,
:conditions => ["created_at < ? AND ( deleted_at IS NULL " +
“OR deleted_at < ? AND deleted_at > ? )”, to, to, from ])
end
end
I have written the find_by_year_month class method on the Asset model
and it works great. When I run this action I get:
NoMethodError in LevelsController#show
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.macro
C:/…/vendor/rails/activerecord/lib/active_record/xml_serialization.rb:
185:in add_includes' C:/../vendor/rails/activerecord/lib/active_record/xml_serialization.rb: 180:in
each’
C:/…/vendor/rails/activerecord/lib/active_record/xml_serialization.rb:
180:in add_includes' C:/../vendor/rails/activerecord/lib/active_record/xml_serialization.rb: 232:in
to_s’
C:/…/vendor/rails/activesupport/lib/active_support/vendor/builder/
xmlbase.rb:140:in call' C:/../vendor/rails/activesupport/lib/active_support/vendor/builder/ xmlbase.rb:140:in
_nested_structures’
C:/…/vendor/rails/activesupport/lib/active_support/vendor/builder/
xmlbase.rb:60:in method_missing' C:/../vendor/rails/activesupport/lib/active_support/vendor/builder/ xmlbase.rb:32:in
send’
C:/…/vendor/rails/activesupport/lib/active_support/vendor/builder/
xmlbase.rb:32:in tag!' C:/../vendor/rails/activerecord/lib/active_record/xml_serialization.rb: 230:in
to_s’
C:/…/vendor/rails/activerecord/lib/active_record/xml_serialization.rb:
107:in to_xml' C:/../app/controllers/schedules_controller.rb:23:in
show’
C:/…/app/controllers/schedules_controller.rb:21:in show' C:/../app/controllers/application.rb:32:in
set_timezone’
Apparently, the error occurs on line 185 of C:/…/vendor/rails/
activerecord/lib/active_record/xml_serialization.rb in `add_includes’
I can’t figure out why this doesn’t work. According to the post Jamis
Buck made (
http://weblog.jamisbuck.org/2006/3/27/web-services-rails-style
) this should work. Shouldn’t it?
Any help would be greatly appreciated.
Nicholas