Forum: Ruby on Rails Strange error...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ad8b580d8004335c95d2d15518168ce3?d=identicon&s=25 Jej (Guest)
on 2009-01-06 17:39
(Received via mailing list)
Hi all,

I get a really strange error... I have 2 simple models:

class Membre < ActiveRecord::Base
  has_one :adhesion
end

class Adhesion < ActiveRecord::Base
  belongs_to :membre
end

The relation works well in the console. I can find a Membre and get
its Adhesion, or find an Adhesion and get the Member.

Now in a controler/view... If I find like this:

@adhesion = Adhesion.find_by_membre_id(id)

I can use @adhesion in the view without any problem.

But if I do:
@adhesion = Membre.find(id).adhesion (what is more clean and logical)

I get an error in the view, as soon as I try to read any field/method
of @adhesion.

What confuses me is that in both cases, the content of @adhesion is
the same in the view:

#<Adhesion id: 1, membre_id: 51, type: nil, date_adhesion: nil,
date_expiration: nil, created_at: "2009-01-06 14:21:06", updated_at:
"2009-01-06 14:21:06">

#<Adhesion id: 1, membre_id: 51, type: nil, date_adhesion: nil,
date_expiration: nil, created_at: "2009-01-06 14:21:06", updated_at:
"2009-01-06 14:21:06">

The failing view:

<%= @adhesion.inspect %>
<%=
if @adhesion.date_adhesion.nil?     # line 9 is here #
  "You don't have adhesion."
%>

The error:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?

/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/
attribute_methods.rb:142:in `create_time_zone_conversion_attribute?'
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/
attribute_methods.rb:75:in `define_attribute_methods'
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/
attribute_methods.rb:71:in `each'
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/
attribute_methods.rb:71:in `define_attribute_methods'
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/
attribute_methods.rb:350:in `respond_to?'
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/
associations/association_proxy.rb:209:in `method_missing'
app/views/membre/adhesion.erb:9

Any idea welcome!

Read you,
Jej
Ad8b580d8004335c95d2d15518168ce3?d=identicon&s=25 Jej (Guest)
on 2009-01-06 17:55
(Received via mailing list)
Well, sorry to make noise! The solution is here:
http://www.railsformers.com/article/activerecord-t...

It seems to be a bug. I just added

config.active_record.default_timezone = :utc

in my env.rb...

Jej
This topic is locked and can not be replied to.