2010-12-04 21 views
2

命名的错误,我想在我加入表在控制器

NameError在 SubscriptionsController#新

未初始化的常量 频道:: ChannelsUser

创建一个记录时出现此错误订阅控制器

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: " [email protected] 
    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 

回答

1

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

我希望这有助于!

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检测t类和关系。

如果你要玩的名称,在控制台中尝试不同的组合:

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

我试过这样做的第二种方式,但没有进展,如果这样做,我必须重新生成文件的第一种方式是如何?第一次我做轨道克模型ChannelsUsers – 2010-12-04 04:23:34

+0

对不起,有一个错字的答案。它应该是`:class_name =>'ChannelUsers'`。我编辑了答案。 – Swanand 2010-12-04 04:25:35