Hello, I am having problem learning how to define relationships. Here is what I have so far class Editora < ActiveRecord::Base has_many :livros end class Livro < ActiveRecord::Base belongs_to :editora end So I wanted to test the relation on the console>> livro = Livro.new( :nome => "Test" )Then I went on to check the relation>> livro.editora_idI expected to get nil, but got "undefined method". So what did I do wrong, and how am I supposed to define and test these relations? Appreciate any help. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Are you sure your database schema includes the proper "editora_id" field in your "livros" table? What''s the exact output when you do a script/console and try to create it?? Does it come out something like this (Notice the @attributes including the editora_id field):>> livro = Livro.new( :nome => "Test" )=> #<Livro:0x344fc34 @new_record=true, @attributes={"nome"=>"Test", "editora_id"=>nil}>>> livro.editora_id=> nil -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Well my schema did not include the “editora_id” field, because I thought rails would take care of it after I declared the relationship on the model. Lazy me, I wanted the rails to do all the work. And thanks you solved my problem :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---