mercredi 10 octobre 2018

Rails 3.2, SQL NoMethodError: undefined method `auth_group='

Full Error Message

1) AuthGroupResourceUser Validations has a valid factory
 Failure/Error: expect(FactoryGirl.build(:auth_group_resource_user)).to be_valid

 NoMethodError:
   undefined method `auth_group=' for #<AuthGroupResourceUser:0x007f929be173c0>
 # /Users/daniel.pan/.rvm/gems/ruby-2.1.2@ssui/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:16:in `public_send'
 # /Users/daniel.pan/.rvm/gems/ruby-2.1.2@ssui/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
 # /Users/daniel.pan/.rvm/gems/ruby-2.1.2@ssui/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
 # /Users/daniel.pan/.rvm/gems/ruby-2.1.2@ssui/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
 # /Users/daniel.pan/.rvm/gems/ruby-2.1.2@ssui/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
 # /Users/daniel.pan/.rvm/gems/ruby-2.1.2@ssui/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
 # /Users/daniel.pan/.rvm/gems/ruby-2.1.2@ssui/gems/factory_girl-4.7.0/lib/factory_girl/evaluation.rb:12:in `object'
 # /Users/daniel.pan/.rvm/gems/ruby-2.1.2@ssui/gems/factory_girl-4.7.0/lib/factory_girl/strategy/build.rb:9:in `result'
 # /Users/daniel.pan/.rvm/gems/ruby-2.1.2@ssui/gems/factory_girl-4.7.0/lib/factory_girl/factory.rb:42:in `run'
 # /Users/daniel.pan/.rvm/gems/ruby-2.1.2@ssui/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
 # /Users/daniel.pan/.rvm/gems/ruby-2.1.2@ssui/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:28:in `run'
 # /Users/daniel.pan/.rvm/gems/ruby-2.1.2@ssui/gems/factory_girl-4.7.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
 # ./spec/models/auth_group_resource_user_spec.rb:12:in `block (3 levels) in <top (required)>'

So I am making some unit tests for my model 'auth_group_resource_user'. It belongs to 'auth_group', and either 'user' or 'auth_role' but not both.

auth_roles sql table

CREATE TABLE IF NOT EXISTS `test`.`auth_roles` (
  `id`  INT(11)      NOT NULL     AUTO_INCREMENT,
  `name`VARCHAR(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
)
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;

auth_groups sql table

CREATE TABLE IF NOT EXISTS `test`.`auth_groups` (
  `id`   INT(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
)
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;

auth_group_resource_user sql table

CREATE TABLE IF NOT EXISTS `test`.`auth_group_resource_users` (
  `id`            INT(11)   NOT NULL AUTO_INCREMENT,
  `auth_group_id` INT(11)   NOT NULL,
  `user_id`       INT(11)   DEFAULT NULL,
  `auth_role_id`  INT(11)   DEFAULT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `auth_group_resource_users_auth_group_id_fk` FOREIGN KEY 
  (`auth_group_id`) REFERENCES `auth_groups` (`id`),
  CONSTRAINT `auth_group_resource_users_user_id_fk` FOREIGN KEY 
  (`user_id`) 
  REFERENCES `users` (`id`),
  CONSTRAINT `auth_group_resource_users_auth_role_id_fk` FOREIGN KEY 
  (`auth_role_id`) REFERENCES `auth_roles` (`id`),
  CONSTRAINT `user_role_check_null` 
  CHECK (
        (`user_id` IS NOT NULL AND `auth_role_id` IS NULL)
    OR  (`user_id` IS NULL AND `auth_role_id` IS NOT NULL))
  )
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci;

AuthGroupResourceUser model is:

 class AuthGroupResourceUser < ActiveRecord::Base

   attr_accessible :auth_group_id

   belongs_to :group_resource, polymorphic: true
   belongs_to :authorizations

   validates :auth_group, presence: true
   validates :user_id, presence: true, unless: :auth_role_id
   validates :auth_role_id, presence: true, unless: :user_id

 end

and the factory is:

 FactoryGirl.define do
   factory :auth_group_resource_user do
     auth_group
     user
   end
 end

auth_group factory looks like

 FactoryGirl.define do
   factory :auth_group do
     name { Faker::Internet.user_name(9) }
   end
 end

For the life of me, I cannot figure out why I'm getting the error above.

Aucun commentaire:

Enregistrer un commentaire