Cannot make relationship work

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_id

I 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.

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

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 :slight_smile: