0
如何創建一個has_many
關聯,但是也能夠通過不是名稱的術語來引用各種實例?有點像類的一個屬性? (dog1
在這種情況下)Rails has_many與單個引用的關聯
class User < ActiveRecord::Base
has_many :dogs
dogs: dog1, dog2, dog3, dog4 # I know this won't work but just to give the idea
end
所以,你可以這樣做:
barry = User.create(name: 'Barry')
barry.dog1.create(name: 'rover', weight: 12)
barry.dog1.name #=> rover
barry.dog1.weight #=> 12