Ruby Sinatra:为什么我的美术师会保存给所有用户,而不仅仅是当前登录的用户?

Ruby Sinatra:为什么我的美术师会保存给所有用户,而不仅仅是当前登录的用户?,ruby,model-view-controller,sinatra,crud,Ruby,Model View Controller,Sinatra,Crud,艺术家对象保存到@artists,但无论我作为哪个用户登录,我都会看到整个列表 模型 class User < ActiveRecord::Base has_many :users_artists has_many :artists, through: :users_artists has_many :artworks, through: :artists end class Artist < ActiveRecord::Base has_many

艺术家对象保存到@artists,但无论我作为哪个用户登录,我都会看到整个列表

模型

class User < ActiveRecord::Base
    has_many :users_artists
    has_many :artists, through: :users_artists
    has_many :artworks, through: :artists
end

class Artist < ActiveRecord::Base
    has_many :artworks
    has_many :users_artists
    has_many :users, through: :users_artists
end

class UserArtists < ActiveRecord::Base 
    belongs_to :user
    belongs_to :artist
  end
在迁移过程中,我多次改变了用户的多元化,感觉自己越来越接近了

class CreateArtists < ActiveRecord::Migration[6.0]
  def change
      create_table :artists do |t|
        t.string :name
        t.text :notes
        t.integer :user_id
        t.timestamps null: false
      end
    end  
end

class CreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      t.text :email
      t.string :password_digest

      t.timestamps null: false
  end
  end
end

class CreateUsersArtists < ActiveRecord::Migration[6.0]
  def change
    create_table :users_artists do |t|
      t.integer :user_id
      t.integer :artist_id
      t.timestamps null: false
    end
  end
end
class CreateArtists这是因为:

@artists = Artist.all
这将加载完整的艺术家列表,这就是您要显示的内容

而是按当前用户对其进行筛选:

@artists = current_user.artists

谢谢,但是当我尝试这样做时,我在/artists uninitialized constant User::UsersArtist处得到了错误名称error。我还尝试将userartist.rb模型的名称更改为UsersArtist类。你知道为什么不确定吗。可能进入rails控制台,检查是否定义了
UsersArtist
,是否可以调用
User.first.artists
。如果更改了文件名,则可能需要更改文件名以匹配类名。
@artists = current_user.artists