我正在学习 ruby on rails 的基础知识,我想做一些简单的查询,但我有疑问:
我将拥有这些模型:
class Client < ActiveRecord::Base
has_one :address
has_many :orders
has_and_belongs_to_many :roles
end
class Address < ActiveRecord::Base
belongs_to :client
end
class Order < ActiveRecord::Base
belongs_to :client, counter_cache: true
end
class Role < ActiveRecord::Base
has_and_belongs_to_many :clients
end
现在,我将使用脚手架来生成所有的东西,我想知道我是否必须直接将外键放在脚手架中,例如:
rails 生成脚手架 地址 street:string number:integer client_id:integer
或者当我建立这些关联然后迁移我的数据库时,它们将是隐式的?
我不知道我是否以最好的方式解释了自己。
谢谢
最佳答案
是的,没有引用。您需要传递 client_id
或对 Client 模型的引用,例如:
rails generate scaffold Address street:string number:integer client_id:integer:index
或
rails generate scaffold Address street:string number:integer client:references
关于ruby-on-rails - Ruby On Rails 脚手架需要包含外键吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26800569/