Rails has_many_through迁移 [英] Rails has_many_through migration

查看:73
本文介绍了Rails has_many_through迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为以下模型创建一个many_to_many关联:

I'm creating a many_to_many association for the following models:

class Competence < ActiveRecord::Base
    has_many :behaviour, through: :behaviours_rel
    has_many :stabilizer, through: :stabilizers_rel
end

class Behaviour < ActiveRecord::Base
    belongs_to :competence
end

class Stabilizer < ActiveRecord::Base
    belongs_to :competence
end

我相信我必须做类似的事情:

I believe I have to do something like:

rails generate migration behaviour:belongs_to 

但是它不起作用.我简直无法理解使用rails generate进行这种迁移的逻辑.

but it doesn't work. I simply can't get the logic of doing this migration with rails generate.

我正在尝试保留几种行为,以至于one_to_many关系还不够.

I'm trying to save in a competence several behaviours so a one_to_many relationship is not enough.

顺便说一句,我不想​​用sql表显式地完成它.

By the way, I don't want to do it explicitly with sql table.

推荐答案

$ rails g model behaviours_rel competence_id:integer behaviour_id:integer

$ rails g model stabilizers_rel competence_id:integer stabilizer_id:integer

$ bundle exec rake db:migrate

请参阅: http://www.codequizzes.com/learn-轨道/多对多关系

这篇关于Rails has_many_through迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆