mercredi 18 mai 2016

NoMethodError: undefined method `invitation'

I have an invitation system that has a before_create callback checking to see if the user has an invitation.

before_create :user_has_invitation

def user_has_invitation
  unless User.invitation.find_by_invite_token(invite_token)
    false
  end
end

USER Model

has_many :invites, :class_name => 'Invitation', :foreign_key => 'sender' #sent_invitations
belongs_to :invitation

INVITATION Model

belongs_to :sender, :class_name => 'User', :foreign_key => 'sender'
has_one :recipient, :class_name => 'User

The error is coming up when I try to seed the database:

#--== Generate Sample Invitations
invite_list = []
invite_list << [ 0, "email@sample.com", "Name", "Lastname", Faker::Internet.password(10), Faker::Date.between(5.days.ago, Date.today) ]
invite_list << [ 1, "test@testing.tld", "name", "lastname", Faker::Internet.password(10), Faker::Date.between(5.days.ago, Date.today) ]
(0..23).map{
  name = Faker::Name.first_name
  surname = Faker::Name.last_name
  invite_list << [ rand(0..5), Faker::Internet.email(name + "." + surname), name, surname, Faker::Internet.password(10), Faker::Date.between(5.days.ago, Date.today) ]
}
invite_list.each do |sender, recipient_email, name, surname, invite_token, sent_at|
  Invitation.create!( sender:@sender, recipient_email:recipient_email, name:name, surname:surname, invite_token:invite_token, sent_at:sent_at )
end

# --== Generate Sample Users
user_list = []
user_list << [ "Name", "Lastname", 't', "email@sample.com", "Password", rand(1..50)]
user_list << [ "name", "lastname", 'f', "test@testing.tld", "Password", rand(1..50)]
(0..7).map{
  name = Faker::Name.first_name
  surname = Faker::Name.last_name
  user_list << [ name, surname, 'f', Faker::Internet.email(name + "." + surname), Faker::Internet.password(10), rand(1..50)]
}
user_list.each do |name, surname, admin, email, password, invitation_id|
  User.find_or_create_by_email!( name:name, surname:surname, admin:admin, email:email, password:password, invitation_id:invitation_id )
  puts "User: #{name} #{surname} created..."
end

Database Schema

create_table "invitations", :force => true do |t|
    t.integer  "sender"
    t.string   "recipient_email"
    t.string   "name"
    t.string   "surname"
    t.string   "invite_token"
    t.datetime "sent_at"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
  end

create_table "users", :force => true do |t|
  t.string   "name"
  t.string   "m_i"
  t.string   "surname"
  t.string   "email"
  t.string   "password_digest"
  t.string   "address"
  t.string   "apt"
  t.string   "city"
  t.string   "state"
  t.string   "zipcode"
  t.string   "auth_token"
  t.string   "reset_token"
  t.datetime "reset_sent"
  t.boolean  "admin"
  t.datetime "created_at",      :null => false
  t.datetime "updated_at",      :null => false
  t.string   "invitation_id"
  t.integer  "invites_avail"
end

This was working before, but I can't figure out the issue. The associations are set in the respective User and Invitation models, so why isn't the User.invitation.find_by_invite_token(invite_token) getting the correct associative data?

Aucun commentaire:

Enregistrer un commentaire