I’m writing my first web app in rails and it was going really well until
now.
I have just linked 2 class with a has_one relationship but its not
working.
class Vmorder < ActiveRecord::Base
has_one :creditcard
…
class Creditcard < ActiveRecord::Base
belongs_to :vmorder
…
mysql> show fields from creditcards;
±-----------------±-------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±-----------------±-------------±-----±----±--------±---------------+
…
| vmorder_id | int(11) | YES | | NULL | |
±-----------------±-------------±-----±----±--------±---------------+
16 rows in set (0.00 sec)
mysql>
this is what I get from the ruby console:
?> vmorder = Vmorder.new
?> creditcard = Creditcard.new
?>
?> vmorder.creditcard = creditcard
NoMethodError: 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.[]
from (eval):1:in id' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1529:in
quoted_id’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_one_association.rb:74:in
construct_sql' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_one_association.rb:6:in
initialize’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations.rb:905:in
`creditcard=’
from (irb):64
I really can’t see what i’m doing wrong here, has anybody got any ideas?
James Sturrock wrote:
±-----------------±-------------±-----±----±--------±---------------+
NoMethodError: You have a nil object when you didn’t expect it!
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_one_association.rb:6:in
initialize' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations.rb:905:in
creditcard=’
from (irb):64
I really can’t see what i’m doing wrong here, has anybody got any ideas?
Worked for me. Trying stripping down your models and migrations to their
bare minimums in a test app and see what you get.
mysql> desc creditcards;
±-----------±-------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±-----------±-------------±-----±----±--------±---------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| vmorder_id | int(11) | YES | | NULL | |
±-----------±-------------±-----±----±--------±---------------+
mysql> desc vmorders;
±------±-------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------±-------------±-----±----±--------±---------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
±------±-------------±-----±----±--------±---------------+
class Vmorder < ActiveRecord::Base
has_one :creditcard
end
class Creditcard < ActiveRecord::Base
belongs_to :vmorder
end
vmorder = Vmorder.new
=> #<Vmorder:0x349a608 @new_record=true, @attributes={“name”=>nil}>
creditcard = Creditcard.new
=> #<Creditcard:0x348a17c @new_record=true, @attributes={“name”=>nil,
“vmorder_id”=>nil}>
vmorder.creditcard = creditcard
=> #<Creditcard:0x348a17c @new_record=true, @attributes={“name”=>nil,
“vmorder_id”=>nil}>
vmorder.creditcard
=> #<Creditcard:0x348a17c @new_record=true, @attributes={“name”=>nil,
“vmorder_id”=>nil}>
–
Michael W.
I have figured out the problem.
in the vmorder class I had an initialise method but I didn’t call
super() from within the method.