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

class Livro < ActiveRecord::Base
belongs_to :editora

So I wanted to test the relation on the console

livro = :nome => “Test” )

Then I went on to check the relation


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 = :nome => “Test” )
=> #<Livro:0x344fc34 @new_record=true, @attributes={“nome”=>“Test”,

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