【问题标题】:Naming error in controller控制器中的命名错误
【发布时间】:2011-05-20 02:34:44
【问题描述】:

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

名称错误在 订阅控制器#new

未初始化的常量 频道::频道用户

订阅控制器

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

渠道模型

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

ChannelsUsers 模型

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

【问题讨论】:

    标签: ruby-on-rails postgresql join


    【解决方案1】:

    您的频道用户类名称是复数。它应该是单数。

    所以你可以改成这样:

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

    或在UserChannel 模型中更改此行:

    has_many :channels_users
    

    has_many :channels_users, :class_name => 'ChannelsUsers'
    

    Rails 将使用 String#classifyString#underscore 等方法来检测类和关系。

    如果您想玩弄这些名称,请在控制台中尝试各种组合:

    >> "channels_users".classify
    => "ChannelsUser"
    >> "ChannelsUser".underscore
    => "channels_user"    
    

    【讨论】:

    • 我尝试过第二种方式,但是如果第一种方式没有进展我必须重新生成文件是怎么回事?我第一次做 rails g model ChannelsUsers
    • 抱歉,答案有误。应该是:class_name =&gt; 'ChannelUsers'。我已经编辑了答案。
    • 现在我收到此错误,在 ChannelsUsers 模型中找不到源关联:subscriber 或 :subscribers。
    • 我用 has_many :subscribers, :class_name => 'Users' , :through => :channels_users , :source => :user 修复了这个问题。但是现在统一常量 Channel::Users 是错误
    【解决方案2】:

    如果您将模型更改为 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

    我希望这会有所帮助!

    【讨论】:

    • 现在按照你的方式做了它未初始化的常量 Channel::ChannelUser
    • 无法使用 has_many :through 但 has_and_belongs_to_many 对我有用,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 2013-02-23
    • 2018-01-07
    • 1970-01-01
    • 2016-07-21
    相关资源
    最近更新 更多