私はこれがモデル協会(has_manyの:経由)レール持って自分のアプリケーションにFactoryGirlでこのモデル関連付けを行う方法は?
class User < ActiveRecord::Base
has_many :assignments
has_many :roles, :through => :assignments
end
class Assignment < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
class Role < ActiveRecord::Base
has_many :assignments
has_many :users, :through => :assignments
end
私は(今のところ)3つの役割を持っているが、それぞれの役割:名前は一意です。したがって、ロールデータベースには、今、ユーザ、管理者、モデレータの3つのエントリが含まれています。
ユーザ、管理者、モデレータ、およびその役割を作成できるように工場を作成するにはどうすればよいですか?
私のような何かをするのが大好きだ:
(役割:名前は一意性のためにチェックされているので、ユーザーごとに1つのロールエントリを作成しないでください、それは代わりに、ユーザーごとの役割に1つの割り当てを作成する必要があります。 )
# Create my roles
FactoryGirl.create(:role, :name => 'admin')
FactoryGirl.create(:role, :name => 'user')
FactoryGirl.create(:role, :name => 'moderator')
# Create 10 users and 10 moderators assuming that the username is sequential or something
for i in 0..10
user = FactoryGirl.create(:user) # user.roles.first.name.eql? 'user' #should be true
mod = FactoryGirl.create(:moderator) # mod.roles.first.name.eql? 'moderator' #should be true
end
FactoryGirlとこの関連付けをモデル化するファクトリをどのように作成しますか?
あなたが私に言ったらどういう意味ですか?「バリデーションを満たすのに役立つユーザーブロックとロールブロック内に列を追加してください」 。また、私の質問で言及した例に準拠するようにコードを修正することもできますか? col1? another_attribute? 、ありがとう:) – Goles