新的rails。我創建了一個包含多個數據庫表的項目,它們之間相互關聯。我想知道如何在創建實例後添加外鍵引用。下面的代碼:Ruby on Rails:多對多實例
在架構:
class Blog < ActiveRecord::Base
has_many :posts
has_many :owners
has_many :users, through: :owners
end
class Owner < ActiveRecord::Base
belongs_to :user
belongs_to :blog
end
class User < ActiveRecord::Base
has_many :messages
has_many :posts
has_many :owners
has_many :blogs, through: :owners
end
型號:
class User < ActiveRecord::Base
has_many :messages
has_many :posts
has_many :owners
has_many :blogs, through: :owners
end
class Owner < ActiveRecord::Base
belongs_to :user
belongs_to :blog
end
class Blog < ActiveRecord::Base
has_many :posts
has_many :owners
has_many :users, through: :owners
end
在軌控制檯:
blog1 = Blog.first
user1 = User.first
blog1.users = user1
NoMethodError: undefined method `each' for #<User:0x0000000487e9f8>