ruby-on-rails - Controller 命名错误

标签 ruby-on-rails postgresql join

尝试在我的连接表中创建记录时出现此错误

NameError in SubscriptionsController#new

uninitialized constant Channel::ChannelsUser

订阅 Controller

class SubscriptionsController < ApplicationController

  helper_method :current_user_session, :current_user
  filter_parameter_logging :password, :password_confirmation

  def new
    @channel = Channel.find(params[:channel_id])
    @user = current_user
    @channel.subscribers << @user
    @channel.save
    flash[:notice] = "You have subscribed to: " +@channel.name
    redirect_to @channel
  end

end

结束

用户模型

class User < ActiveRecord::Base

  acts_as_authentic

  ROLES = %w[admin  moderator subscriber]

  #Each user can subscribe to many channels
  has_many :channels_users
  has_many :subscriptions, :class_name => "Channel", :through => :channels_users

  #Each user who is a moderator can moderate many channels
  has_many :channel_mods
  has_many :channels, :through => :channel_mods

  #Each user can receive many messages
  has_many :messages_users , :dependent => :destroy
  has_many :reciepts , :class_name => "User", :through => :messages_users

  #Filter users by role(s)
  named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} }

  def roles  
    ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }  
  end

  def roles=(roles)  
    self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum  
  end

  def role_symbols
    role.map do |role|
      role.name.underscore.to_sym
    end
  end

end

channel 模型

class Channel < ActiveRecord::Base
  #Each channel owns many or no messages
  has_many :messages
  #Each channel is own by one moderator
  has_many :channel_mods
  has_many :moderators, :class_name =>'User', :through =>:channel_mod
  #Each channel can have and belong to many or no users
  has_many :channels_users
  has_many :subscribers, :class_name => 'Users' , :through => :channels_users

end

channel 用户模型

class ChannelsUsers < ActiveRecord::Base
  belongs_to :user
  belongs_to :channel
end

最佳答案

如果将模型更改为 ChannelUser,这会更好读。对应关系如下:

class Channel < ActiveRecord::Base
  has_many :channel_users
  has_many :users, :through => :channel_users
end

class User < ActiveRecord::Base
  has_many :channel_users
  has_many :channels, :through => :channel_users
end

class ChannelUser < ActiveRecord::Base
  belongs_to :channel
  belongs_to :user
end

您的连接表将被称为 channel_users。我认为您最初将其命名为 channels_users 是因为这是 has_and_belongs_to_many 连接表的设置。但由于您使用的是 has_many :through,因此您可以随意为表命名。

我今年早些时候写了一篇博客文章,详细介绍了所有选项:

Basic Many-to-Many Associations in Rails

希望对您有所帮助!

关于ruby-on-rails - Controller 命名错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4351858/

相关文章:

Postgresql如何根据主选择中的值在选择中进行子选择

mysql - 根据表A中的数组值从表B中选择多行

php - SQL JOIN 与 CakePHP

ruby-on-rails - 使用 Ruby on Rails 设置 Leaflet

ruby-on-rails - 在rails中,为模型创建factorygirl时,不应该调用模型的before_create吗?

javascript - rails : Internationalization of Javascript Strings?

ruby-on-rails - 如何将文件隐私(公共(public)或私有(private))添加到 rails 中具有事件存储的谷歌云存储文件?

database - 如果 postgres 的性能低下,我应该选择哪个数据库

json - Postgresql更新json数据属性

sql - Ruby ActiveRecord 通过关联进行多重联接