gpt4 book ai didi

ruby-on-rails-3 - 如何正确使用Factory_girl和has_many 'through'模型

转载 作者:行者123 更新时间:2023-12-02 08:56:16 24 4
gpt4 key购买 nike

我在理解 Factory Girl 中的回调模型和方法时遇到了一些困难。使用这篇文章 http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl 我开始尝试一下,现在我在规范中出错了。我是 Rails 新手...所以我可能会错过一些非常基本的东西。

错误是:失败/错误:@user = Factory(:admin_user)# 的未定义方法“each”

模型:

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

# Setup accessible (or protected) attributes for your model
attr_accessible :username, :email, :role_ids, :password, :password_confirmation, :remember_me
has_many :userroles
has_many :roles, :through=>:userroles


def role?(role)
return !!self.roles.find_by_name(role.to_s.camelize)
end
end

class Role < ActiveRecord::Base
attr_accessible :name

validates :name, :presence=>true,
:length=>{:minimum=>4, :maximum=>30}

has_many :userroles
has_many :users, :through=>:userroles
end

class Userrole < ActiveRecord::Base
attr_accessible :user_id, :role_id
belongs_to :user
belongs_to :role
end

工厂:

Factory.define :user do |f|
f.email '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cab9a5a7afa5a4af8ab9a5a7afa9a5a7baaba4b3e4a9a5a7" rel="noreferrer noopener nofollow">[email protected]</a>'
f.username 'foo'
f.password '123456'
end

Factory.define :role do |r|
r.name 'testrole'
end

Factory.define :userrole do |ur|
ur.association(:role)
ur.userrole{|ur| ur.userrole(:user)}
end
Factory.define :admin_role, :class => 'Role' do |userrole|
userrole.role { |role| role.association(:role, :name => "Admin") }
end

Factory.define :admin_user, :parent => :user do |user|
user.after_create { |u| Factory(:admin_role, :userroles => u) }
end

最后是简单的规范:

require File.dirname(__FILE__) + '/../spec_helper'

describe RolesController do
fixtures :all
include Devise::TestHelpers
render_views

before (:each) do
@user = Factory(:admin_user)
sign_in @user
end

it "index action should render index template" do
get :index
response.should render_template(:index)
end
end

更新:添加堆栈跟踪

ruby-1.9.2-p136 :001 > u=Factory(:admin_user)
NoMethodError: undefined method `each' for #<User:0x00000004d737d8>
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:364:in `method_missing'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:46:in `method_missing'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:352:in `replace'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/activerecord-3.0.3/lib/active_record/associations.rb:1524:in `block in collection_accessor_methods'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy/build.rb:13:in `set'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:320:in `block in run'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:320:in `each'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:320:in `run'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:270:in `create'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:301:in `default_strategy'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl.rb:20:in `Factory'
from /home/ryan/Sites/Holocron/spec/factories/users.rb:22:in `block (2 levels) in <top (required)>'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy.rb:29:in `call'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy.rb:29:in `block in run_callbacks'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy.rb:28:in `each'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy.rb:28:in `run_callbacks'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy/create.rb:7:in `result'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:327:in `run'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:270:in `create'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:301:in `default_strategy'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/factory_girl-1.3.2/lib/factory_girl.rb:20:in `Factory'
from (irb):1
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136@Holocron/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

最佳答案

我相信你自己也很困惑。

您已将 Factory.define :admin_role, :class => 'Role' do |userrole| 定义为 Role,但随后在工厂定义中将其视为就像用户角色。 (顺便说一句,Ruby 约定使用下划线 - 因此 Userrole 将是 UserRole/user_role。)

此外,您的user.after_create { |u| Factory(:admin_role, :userroles => u) } 可能应该是: user.after_create { |u| Factory(:admin_role, :userroles => [u]) } 因为 Role has_many :userroles

关于ruby-on-rails-3 - 如何正确使用Factory_girl和has_many 'through'模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4660496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com