1
votes

I have two models Users and Accounts which has the relationship

Users has_many Accounts via `created_by_id`

users.rb

FactoryGirl.define do
  factory :user do
    first_name {Faker::Name.first_name}
    last_name {Faker::Name.last_name}
    email {Faker::Internet.email}
    username {Faker::Internet.user_name}
    password {Faker::Internet.password}
  end

end

accounts.rb

FactoryGirl.define do
  factory :account do
    name {Faker::Company.name}
    association :user
  end

end

Models

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable,
         :validatable, :confirmable, :invitable

  has_many :accounts

  # validations
  validates :first_name, presence: true
  validates :last_name, presence: true
  validates :email, presence: true,
                    uniqueness: {case_sensitive: false},
                    :email => true
  validates :username, uniqueness: {case_sensitive: false, allow_blank: true}

  before_validation :downcase_attributes


  def name
    "#{first_name} #{last_name}"
  end

  private

  def downcase_attributes
    self.email = email.try(:downcase)
    self.username = username.try(:downcase)
  end
end

Accounts Model

class Account < ActiveRecord::Base

  extend FriendlyId
  friendly_id :name, use: :slugged

  belongs_to :users, foreign_key: "created_by_id"


  # Methods

  def should_generate_new_friendly_id?
    name_changed?
    end

end

When i try to Testing the Validity of the Factories by the following

require 'rails_helper'

describe Account, type: :model do

  context "valid Factory" do
    it "has a valid factory" do
      expect(build(:account)).to be_valid
    end
  end

  context "validations" do
    before { create(:account) }

    context "presence" do
      it { should validate_presence_of :name }
    end

  end

end

Error

Account
  valid Factory
    has a valid factory (FAILED - 1)
  validations
    presence
      example at ./spec/models/account_spec.rb:15 (FAILED - 2)

Failures:

  1) Account valid Factory has a valid factory
     Failure/Error: expect(build(:account)).to be_valid
     NoMethodError:
       undefined method `user=' for #<Account:0x007f858a4783a0>
     # ./spec/models/account_spec.rb:7:in `block (3 levels) in <top (required)>'

  2) Account validations presence
     Failure/Error: before { create(:account) }
     NoMethodError:
       undefined method `user=' for #<Account:0x007f8595154088>
     # ./spec/models/account_spec.rb:12:in `block (3 levels) in <top (required)>'

Finished in 0.87731 seconds (files took 5.61 seconds to load)
2 examples, 2 failures

Failed examples:

rspec ./spec/models/account_spec.rb:6 # Account valid Factory has a valid factory
rspec ./spec/models/account_spec.rb:15 # Account validations presence
1
Try changing belongs_to :users, foreign_key: "created_by_id" to belongs_to :user, foreign_key: "created_by_id"Pavan

1 Answers

1
votes

This line:

belongs_to :users, foreign_key: "created_by_id"

should be changed to:

 belongs_to :user, foreign_key: :created_by_id # notice singular :user

From docs:

A belongs_to association sets up a one-to-one connection with another model, such that each instance of the declaring model "belongs to" one instance of the other model. ...