Ruby on rails ActiveRecord::语句在Usersindex中无效?HABTM。
我为文章和用户搭建了脚手架。当我试图检索用户索引上的项目名称时,收到此错误消息。我认为这是说联接表不存在。如何解决这个问题 错误内容如下:Ruby on rails ActiveRecord::语句在Usersindex中无效?HABTM。,ruby-on-rails,Ruby On Rails,我为文章和用户搭建了脚手架。当我试图检索用户索引上的项目名称时,收到此错误消息。我认为这是说联接表不存在。如何解决这个问题 错误内容如下: ActiveRecord::StatementInvalid in Users#index PG::UndefinedTable: ERROR: relation "articles_users" does not exist LINE 5: WHERE a.attrelid =
ActiveRecord::StatementInvalid in Users#index
PG::UndefinedTable: ERROR: relation "articles_users" does not
exist
LINE 5: WHERE a.attrelid =
'"articles_users"'::regcla...
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"articles_users"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
用户表
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.timestamps null: false
end
end
end
条款表:
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.string :article_name
t.timestamps null: false
end
end
end
联接表:
class CreateArticlesUsersJoin < ActiveRecord::Migration
def change
create_table :articles_users_joins do |t|
t.belongs_to :article, index: true
t.belongs_to :user, index: true
end
end
end
第条协会:
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :articles
end
用户关联:
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :articles
end
默认情况下,HABTM使用两个模型名作为联接表 在您的情况下,HABTM默认使用articles_用户作为联接表 如果您想使用另一个表作为联接表,只需使用:Join\u table选项
class Article < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => "articles_users_joins"
end
class User < ActiveRecord::Base
has_and_belongs_to_many :articles, :join_table => "articles_users_joins"
end