关系模型不能按预期工作

时间:2018-06-03 21:38:08

标签: ruby-on-rails activerecord

我使用迁移创建了以下Active Record Schema,但这些关系并不对应于架构。我已经尝试过重置,删除,创建和迁移但是在Rails C中,如果我创建了一个用户u.User.create!(...),然后查询u.groups或u.genres我得到了'未定义的方法'

感谢您的帮助

ActiveRecord::Schema.define(version: 20180603211047) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "genres", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "tag"
    t.bigint "user_id"
    t.index ["user_id"], name: "index_genres_on_user_id"
  end

  create_table "genres_users", id: false, force: :cascade do |t|
    t.bigint "user_id", null: false
    t.bigint "genre_id", null: false
  end

  create_table "groups", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "name"
    t.bigint "user_id"
    t.index ["user_id"], name: "index_groups_on_user_id"
  end

  create_table "groups_users", id: false, force: :cascade do |t|
    t.bigint "user_id", null: false
    t.bigint "group_id", null: false
  end

  create_table "playlists", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "name"
    t.string "link"
    t.text "description"
    t.bigint "group_id"
    t.index ["group_id"], name: "index_playlists_on_group_id"
  end

  create_table "users", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.string "name"
    t.string "token"
    t.date "birthday"
    t.string "link"
    t.string "playlistId"
    t.string "country"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "genres", "users"
  add_foreign_key "groups", "users"
  add_foreign_key "playlists", "groups"
end

以下是模型:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
    #before_action :authenticate_user!
    has_and_belongs_to_many :genres, :through => :genres_users
    has_and_belongs_to_many :groups, :through => :groups_users
    include Enumerable
end

class Genre < ApplicationRecord
    has_and_belongs_to_many :users, :through => :genres_users
end

class Group < ApplicationRecord
    has_and_belongs_to_many :users, :through => :groups_users
    has_one :playlist
end

class Playlist < ApplicationRecord
    belongs_to :group
end

关系是群组拥有用户,用户拥有流派(最喜欢的流派!),这些流派拥有并属于通过连接表的关系(每个用户有多个流派,每个用户有多个群组)。每个组都有一个播放列表,并且会有多个播放列表

1 个答案:

答案 0 :(得分:0)

[OP澄清后编辑]

  

关系是群组拥有用户,用户拥有流派(最喜欢的流派!),这些流派拥有并属于通过连接表的关系(每个用户有多个流派,每个用户有多个群组)。每个组都有一个播放列表,并且会有多个播放列表

首先,您不需要关于群组或流派的user_id列,因为这不是设置应该如何运作。

class Genre < ApplicationRecord
  has_many :favorite_genres
  has_many :users, through: :favorite_genres
  [... other stuff]
end


class User < ApplicationRecord
  has_many :group_memberships
  has_many :groups, through: :group_memberships

  has_many :favorite_genres
  has_many :users, through: :favorite_genres

  [... other stuff]
end

class Group < ApplicationRecord
  has_many :group_memberships
  has_many :users, through: :group_memberships

  has_many :playlists
  [... other stuff]
end

class Playlist < ApplicationRecord
  belongs_to :group
end

class GroupMemberships < ApplicationRecord
  belongs_to :user
  belongs_to :group
  [... other stuff]
end

class FavoriteGenres < ApplicationRecord
  belongs_to :user
  belongs_to :genre
  [... other stuff]
end

因此,您要将user_id列放在组中。连接发生在:group_memberships(以前称为users_groups的表),即user_id,group_id,然后您可以根据需要添加其他元数据列(例如,admin boolean / role等) 。这被称为&#34;有很多通过&#34;关系(http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

同样,用户最喜欢的类型是通过直接关系设置的。因此,您将通过联接为这些提供单独的数据库表和模型文件。

我认为您根本不需要add_foreign_key这个级别的电话,也不需要很多索引。您可能会更加热切地加载或者可能在连接表上添加索引,并且您在模式中执行以下操作:

t.index ["user_id", "genre_id"], name: "index_favorite_genres_on_user_id_and_genre_id"

请记住belongs_to现在为5.x中存在的验证创建验证。您可以通过在模型中的该行上添加optional: true来覆盖此内容,例如belongs_to :foo, optional: true

所有这一切,这就是你的新架构:

create_table "genres", id: :serial, force: :cascade do |t|
  t.string "tag"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

create_table "groups", id: :serial, force: :cascade do |t|
  t.string "name"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

create_table "favorite_genres", id: false, force: :cascade do |t|
  t.bigint "user_id", null: false
  t.bigint "genre_id", null: false
end

create_table "groups_memberships", id: false, force: :cascade do |t|
  t.bigint "user_id", null: false
  t.bigint "group_id", null: false
end

create_table "playlists", id: :serial, force: :cascade do |t|
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "name"
  t.string "link"
  t.text "description"
  t.bigint "group_id"
  t.index ["group_id"], name: "index_playlists_on_group_id"
end

create_table "users", id: :serial, force: :cascade do |t|
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "email", default: "", null: false
  t.string "encrypted_password", default: "", null: false
  t.string "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer "sign_in_count", default: 0, null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string "current_sign_in_ip"
  t.string "last_sign_in_ip"
  t.string "name"
  t.string "token"
  t.date "birthday"
  t.string "link"
  t.string "playlistId"
  t.string "country"
  t.index ["email"], name: "index_users_on_email", unique: true
  t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

给它一个旋转(我没有在应用程序中构建它,所以代码中可能有一些错误)你现在应该可以运行控制台了:

u = User.create([values])
u.genres (should return nil until you create some relationships)

等。