rails3.2,sql nomethoderror:未定义的方法`auth\u group='

2ul0zpep  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(244)

完整错误消息

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)>'

所以我正在为我的模型“auth\u group\u resource\u user”做一些单元测试。它属于“auth\u group”和“user”或“auth\u role”,但不能同时属于两者。
身份验证角色sql表

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;

验证组sql表

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;

身份验证组资源用户sql表

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模型为:

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

工厂是:

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

auth\ U集团工厂看起来像

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

就我的一生而言,我不明白为什么我会犯上述错误。

yqhsw0fo

yqhsw0fo1#

结果只是把关系改成了

belongs_to :auth_group
belongs_to :user
belongs_to :auth_role

而且成功了。。。

相关问题