class User
has_many :mads
def mad
Mad.find_by_user_id(id)
end
end
class God
belongs_to :mad
belongs_to :user
belongs_to :sender, :class_name => "User", :foreign_key =>
"sender_id"
def strange_change!
God.transaction do
puts mad.nil? #the first time put is false
if mad.nil?
puts ''some thing exctue here'' #no puts
anything
mad = you.mad
end
puts mad.nil? #the second time put is true
end
end
end
why the mad will become nil?
--
Posted via http://www.ruby-forum.com/.
On Wed, Sep 15, 2010 at 3:19 AM, Zhenning Guan <lists at ruby-forum.com> wrote:> class User > ?has_many :mads > > ?def mad > ? ?Mad.find_by_user_id(id) > ?end > end > > class God > ?belongs_to :mad > ?belongs_to :user > ?belongs_to :sender, :class_name => "User", :foreign_key => "sender_id" > > ?def strange_change! > ? ?God.transaction do > ? ? ?puts mad.nil? ? ?#the first time put is false > ? ? ?if mad.nil? > ? ? ? ?puts ''some thing exctue here'' ? ? ? ? ? #no puts anything > ? ? ? ?mad = you.mad > ? ? ?end > ? ? ?puts mad.nil? ? ?#the second time put is true > ? ?end > ?end > end > > why the mad will become nil?Seems like a Rails question, not an RSpec question. Please post Rails questions to http://groups.google.com/group/rubyonrails-talk. Cheers, David
Typical Ruby gotcha:
God.transaction do
puts mad.nil? # mad is instance method of God and returns
BelongsToAssociation here
if mad.nil?
puts ''some thing exctue here''
mad = you.mad # Ruby sees mad local variable definition
end
puts mad.nil? # mad is unitialized local variable set to nil
end
On Wed, Sep 15, 2010 at 5:22 PM, David Chelimsky <dchelimsky at gmail.com>
wrote:> On Wed, Sep 15, 2010 at 3:19 AM, Zhenning Guan <lists at
ruby-forum.com> wrote:
>> class User
>> ?has_many :mads
>>
>> ?def mad
>> ? ?Mad.find_by_user_id(id)
>> ?end
>> end
>>
>> class God
>> ?belongs_to :mad
>> ?belongs_to :user
>> ?belongs_to :sender, :class_name => "User", :foreign_key
=> "sender_id"
>>
>> ?def strange_change!
>> ? ?God.transaction do
>> ? ? ?puts mad.nil? ? ?#the first time put is false
>> ? ? ?if mad.nil?
>> ? ? ? ?puts ''some thing exctue here'' ? ? ? ? ? #no
puts anything
>> ? ? ? ?mad = you.mad
>> ? ? ?end
>> ? ? ?puts mad.nil? ? ?#the second time put is true
>> ? ?end
>> ?end
>> end
>>
>> why the mad will become nil?
>
> Seems like a Rails question, not an RSpec question. Please post Rails
> questions to http://groups.google.com/group/rubyonrails-talk.
>
> Cheers,
> David
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>