gpt4 book ai didi

ruby-on-rails - has_many :through uninitialized constant

转载 作者:行者123 更新时间:2023-12-04 02:58:57 31 4
gpt4 key购买 nike

我已经阅读了有关 has_many 的文档和大量教程:通过 Rails 中的关系,但我终生无法掌握它的窍门。

我正在尝试向我的 current_user(devise) 添加一个组,并且我在 Group 之间有一个表和 User带有状态(该组的用户状态是可变的)。

现在每当我创建一个新组时,我都会收到一条错误消息 uninitialized constant Group::GroupUser
这是我的模型:

groupuser.rb

class GroupUser < ActiveRecord::Base
belongs_to :group
belongs_to :user
end

group.rb
class Group < ActiveRecord::Base
has_many :clients
has_and_belongs_to_many :pictograms

has_many :group_users
has_many :users, :through => :group_users

accepts_nested_attributes_for :clients

validates_length_of :name, :minimum => 5
validates_presence_of :name
validates_presence_of :background
validates_presence_of :clocktype
end

用户.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable


validates_presence_of :first_name
validates_presence_of :last_name
validates :email, presence: true, uniqueness: true

has_many :group_users
has_many :groups, :through => :group_users

has_attached_file :avatar, :styles => {
:medium => "300x300#",
:thumb => "100x100#"
}
validates_attachment_content_type :avatar, :content_type => ['image/jpg', 'image/png', 'image/jpeg']

validates_attachment :avatar,
:size => { :in => 0..1.megabytes }

def completeName
"#{self.first_name} #{self.last_name}"
end
end

以及来自 的相关内容schema.rb
  create_table "group_users", id: false, force: true do |t|
t.integer "group_id"
t.integer "user_id"
t.integer "status", default: 0
end

add_index "group_users", ["group_id"], name: "index_group_users_on_group_id"
add_index "group_users", ["user_id"], name: "index_group_users_on_user_id"

create_table "groups", force: true do |t|
t.string "name"
t.integer "clocktype"
t.string "background"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "first_name"
t.string "last_name"
t.string "password"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.datetime "created_at"
t.datetime "updated_at"
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
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

最后。 引发错误的行
@group.users << current_user

最佳答案

我能看到的问题是你对 GroupUser 的命名

我们一直使用连接模型,我们也用下划线命名它们:

group_user.rb
class GroupUser < ActiveRecord::Base
belongs_to :group
belongs_to :user
end

未初始化的常量错误将由模型未正确加载引起,我想这将是由于命名问题。如果这不起作用,您应该使用 :class_name 引用模型。关系声明中的参数:
  has_many :group_users, :class_name => 'GroupUser'
has_many :groups, :through => :group_users

更新

We wanted to call extra attributes从连接模型,并找到这种方式:
#app/models/message.rb
has_many :image_messages, :class_name => 'ImageMessage'
has_many :images, -> { select("#{Image.table_name}.*, #{ImageMessage.table_name}.caption AS caption") }, :class_name => 'Image', :through => :image_messages, dependent: :destroy

这使用 select创建 ActiveRecord 关联时可以使用的方法,正如我们 discovered from this RailsCast ,您可以使用它来创建 alias查询中的列

对你来说,你不妨试试这个:
#app/models/user.rb
has_many :group_users, :class_name => 'Groupuser'
has_many :groups, -> { select("#{User.table_name}.*, #{Groupuser.table_name}.status AS status") }, :class_name => 'Group', :through => :group_users, dependent: :destroy

关于ruby-on-rails - has_many :through uninitialized constant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20607327/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com