2
votes

I am trying to seed my database with some values. Types User, Account, and Transaction are ActiveRecord objects. Two users are created successfully and two accounts on those users are created successfully. When I try to do the same thing in the second block, it tells me NoMethodError: undefined method 'transactions' for #<Array:0x0000000734d198>, even though a puts inside the second block shows that account is in fact a #.

require 'securerandom'

Account.destroy_all
Branch.destroy_all
Terminal.destroy_all
Transaction.destroy_all
User.destroy_all


users = User.create!([
                         {
                             :name => 'Bob Rob',
                             :email => '[email protected]'
                         }, {
                             :name => 'Frank Krawlin',
                             :email => '[email protected]'
                         }
                     ])

puts users

accounts = []
users.each do |user|
  accounts << user.accounts.create!([{:account_type => 'SAVINGS'}])
end

puts accounts

transactions = []
accounts.each do |account|
  transactions << account.transactions.create!([{:transaction_type => 'WITHDRAWAL', :amount => 500, :expires_at => Time.now + 1.day, :fulfilled => false}])
end

puts transactions

branches = Branch.create!([{:address => '215 Oxford St W, Toronto, ON'}])

terminals = []
branches.each do |branch|
  rand(1..4).times do
    terminals << branch.terminals.create!([{:token => SecureRandom.uuid}])
  end
end

Here is the account.rb file where transactions is defined as a has_many relationship:

class Account < ApplicationRecord
  belongs_to :user
  has_many :transactions

  validates :account_type, inclusion: {in: %w(CHEQUING SAVINGS),
                                       message: "%{value} is not a valid account type"}

  before_validation :correct_case

  protected
  def correct_case
    self.account_type.upcase!
  end
end

I have no idea what is going on. transactions is definitely defined as has_many on Account. What is causing this error? I am very new to ruby.

1
Would you mind to post how have you populated users and accounts? - Ed de Almeida
@EddeAlmeida I tried creating each user individually, but they were never a problem. I debugged the output and the users were created just fine. The accounts are created without issue as well. It's the transactions that are giving me the problem. - user3096803
This is not easy to reproduce here, then I'll ask you to do some tests. Fisrt, would you please try doing account.transaction.create! (singular)? - Ed de Almeida
@EddeAlmeida It still says NoMethodError, only this time it says NoMethodError: undefined method `transaction' for #<Array:0x000000073d8dd8>. I have even tried swapping the line out with Transaction.create! and passing account.id but then I get NoMethodError: undefined method 'id' for #<Array:0x0000000724c960>. - user3096803
Insert a puts account.class right after accounts.each do |account| and tell me what is the result, please. - Ed de Almeida

1 Answers

0
votes

When you wrote

accounts << user.accounts.create!([{:account_type => 'SAVINGS'}])

above, you created not a single account, but an array with a single account. Then, when you say

transactions << account.transactions.create!([{:transaction_type => 'WITHDRAWAL', :amount => 500, :expires_at => Time.now + 1.day, :fulfilled => false}])

rails thinks you are trying to use a method #transactions from an array, which obviously can't be done, since array has no such method.