简体   繁体   English

Rails 3.2,SQL NoMethodError:未定义方法“ auth_group =”

[英]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'. 因此,我正在为模型“ auth_group_resource_user”进行一些单元测试。 It belongs to 'auth_group', and either 'user' or 'auth_role' but not both. 它属于“ auth_group”,并且属于“ user”或“ auth_role”,但不能同时属于两者。

auth_roles sql table auth_roles 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;

auth_groups sql table auth_groups 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;

auth_group_resource_user sql table auth_group_resource_user 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 model is: 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

and the factory is: 工厂是:

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

auth_group factory looks like auth_group工厂看起来像

 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. 对于我的一生,我无法弄清楚为什么会出现上述错误。

Ended up just changing relationship to 最终只是改变了与

belongs_to :auth_group
belongs_to :user
belongs_to :auth_role

and it worked... 而且有效...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 3.2 + ruby​​2 + mysql2 + NoMethodError:未定义的方法&#39;query&#39;为nil:NilClass - Rails 3.2 + ruby2 + mysql2 + NoMethodError: undefined method `query' for nil:NilClass lynda Rails Tut:NoMethodError:#的未定义方法“页面” - lynda Rails Tut: NoMethodError: undefined method `page' for #<Subject:0x rake db:create提供了NoMethodError:Rails:Module的未定义方法&#39;init&#39; - rake db:create gives NoMethodError: undefined method `init' for Rails:Module ruby/sql 语法未定义方法“查询”为 nil:NilClass (NoMethodError) - ruby/sql syntax undefined method `query' for nil:NilClass (NoMethodError) NoMethodError:更新为Ubuntu 12.04后,Rails中Mysql:Class的未定义方法&#39;init&#39; - NoMethodError: undefined method `init' for Mysql:Class in rails after updating to ubuntu 12.04 NoMethodError:#的未定义方法`more_results&#39; <Mysql> - NoMethodError: undefined method `more_results' for #<Mysql> 带有变量的Rails动态SQL:未定义的方法find_by_SQL - Rails Dynamic SQL with variables: undefined method find_by_SQL DataMapper NoMethodError:DateTime的未定义方法“ encode_json” - DataMapper NoMethodError: undefined method `encode_json' for DateTime Rails 3.2-使用在属性上调用的方法的ActiveRecord`where`查询 - Rails 3.2 - ActiveRecord `where` query with method called on attribute Ruby SQL:未定义的方法“ []” - Ruby SQL: undefined method `[]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM