Rails 中的 HABTM 和连接表问题

发布于 2024-09-10 13:45:09 字数 1587 浏览 4 评论 0原文

我有一个简单的模型:

class User < ActiveRecord::Base
   has_and_belongs_to_many :roles
end

class Role < ActiveRecord::Base
   has_and_belongs_to_many :users
end

我创建了一个简单的连接表:

class CreateUsersRoles < ActiveRecord::Migration
  def self.up
    create_table :users_roles, :id => false do |t|
      t.integer :user_id
      t.integer :role_id

      t.timestamps
    end
  end

  def self.down
    drop_table :users_roles
  end
end

迁移后,shema.rb 如下:

create_table "roles", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
end

create_table "users", :force => true do |t|
    t.string   "login"
    t.string   "password"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

create_table "users_roles", :id => false, :force => true do |t|
  t.integer  "user_id"
  t.integer  "role_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

此处的条目以相同的顺序呈现,与“schema.rb”中一样,

我按以下方式排列装置:

# roles.yml 
user:
  name: user

admin:
  name: admin

moderator:
  name: moderator

# users.yml
User1:
  login: User1
  password: User1
  roles: user

User2:
  login: User2
  password: User2
  roles: admin, moderator

User3:
  login: User3
  password: User3
  roles: moderator

并得到了问题:在“rake db:fixtures:load”上,rails 抱怨连接表的名称:

SQLite3::SQLException:没有这样的表:roles_users:从“roles_users”WHERE 1=1 中删除

问题是 - 为什么它需要“roles_users”,而表是“users_roles”?

I have a simple model:

class User < ActiveRecord::Base
   has_and_belongs_to_many :roles
end

class Role < ActiveRecord::Base
   has_and_belongs_to_many :users
end

I have created a simple join table:

class CreateUsersRoles < ActiveRecord::Migration
  def self.up
    create_table :users_roles, :id => false do |t|
      t.integer :user_id
      t.integer :role_id

      t.timestamps
    end
  end

  def self.down
    drop_table :users_roles
  end
end

After migration, shema.rb is following:

create_table "roles", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
end

create_table "users", :force => true do |t|
    t.string   "login"
    t.string   "password"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

create_table "users_roles", :id => false, :force => true do |t|
  t.integer  "user_id"
  t.integer  "role_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

The entries are presented here in the same order, as in "schema.rb"

I arranged fixtures in following way:

# roles.yml 
user:
  name: user

admin:
  name: admin

moderator:
  name: moderator

# users.yml
User1:
  login: User1
  password: User1
  roles: user

User2:
  login: User2
  password: User2
  roles: admin, moderator

User3:
  login: User3
  password: User3
  roles: moderator

And got a problem: on "rake db:fixtures:load" rails complains about name of a join table:

SQLite3::SQLException: no such table: roles_users: DELETE FROM "roles_users" WHERE 1=1

The question is - why it expects "roles_users", while the table is "users_roles" ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花易冷人易散 2024-09-17 13:45:09

当 Rails 创建连接表的关联时,它期望模型按字母顺序排列。由于 r 位于 u 之前,因此它将其创建为 Roles_users。

我建议您重命名连接表,或者,您可以添加 :join_table => “user_roles” 到两个关联。

When Rails creates the association for the join table it expects the models to be in alphabetical order. Since r comes before u, it creates it as roles_users.

I suggest that you rename the join table, alternatively, you can add :join_table => "users_roles" to both associations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文