我必须建模User
和Company
. 和协会如:
class User < ActiveRecord::Base
has_and_belongs_to_many :companies
end
和
class Company < ActiveRecord::Base
has_and_belongs_to_many :users
end
在迁移中创建连接表:
class CreateCompaniesUsers < ActiveRecord::Migration
def change
create_table :companies_users, :id => false do |t|
t.references :company
t.references :user
t.timestamps
end
end
end
它工作正常,当:
user = User.new(:name => "name", :email => "email@em.com" :password => "pass")
user.companies.find_or_create_by_name(:name => "name")
user.save
它不起作用,(但将数据插入users
and companies
)在以下情况下:
user = User.first # or by finding some existing user
user.companies.find_or_create_by_name(:name => "name")
此外,它还显示一条消息:
Creating scope :page. Overwriting existing method Company.page.
请帮助我摆脱困境。谢谢。