James Sturrock wrote:> 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?
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 Wang
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---