mercredi 31 août 2016

Rails Associations don't work

I've read through many tutorials, and copied their code exactly, yet what they claim works for them doesn't work for me.

I'm making a most basic "has_many" and "belongs_to" association, but rails refuses to acknowledge any association whatsoever.

A user "has_many" emails. Emails "belong_to" user. Here's my code:

user.rb

class User < ActiveRecord::Base
  unloadable

  has_many :emails
  accepts_nested_attributes_for :emails,
    :allow_destroy => true,
  #  :reject_if     => :all_blank
end

email.rb

class Email < ActiveRecord::Base
  unloadable

  belongs_to :user
end

Then, in the console:

User.emails.build
NoMethodError: undefined method `emails' for #<Class:0x00000006c16e88>

Indeed, this "NoMethodError" persists no matter what.

As of now, my guess is that a capacitor in my hardware burnt out while I was installing rails, causing everything to work except this one thing. Or maybe it's something else :p

Aucun commentaire:

Enregistrer un commentaire