我有以下型号:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
has_many :rulesets
end
class Ruleset < ActiveRecord::Base
attr_accessible :title, :game_id, :user_id
validates :game_id, presence: true
validates :user_id, presence: true
validates :title, presence: true
belongs_to :user
belongs_to :game
has_many :rules
end
class Rule < ActiveRecord::Base
attr_accessible :description, :ruleset_id
belongs_to :ruleset
validates :description, presence: true
validates :ruleset_id, presence: true
end
我有一个名为 PagesController 的控制器,它控制用户仪表板,我想在其中显示用户拥有的规则集数量和规则数量。这是我的控制器
class PagesController < ApplicationController
def home
end
def dashboard
@rulesets = current_user.rulesets
end
end
在我的仪表板视图中,我试图显示规则集和规则计数:
<% if current_user.rulesets.any? %>
<li><%= @rulesets.count %> Ruleset</li>
<li><%= @rulesets.rules.count%> Rules</li>
<% end %>
如果我只是尝试计算规则集,这将返回正确数量的规则集。当我尝试计算规则时,我得到这个和“未定义的方法‘规则’”错误。我应该如何访问用户规则集中的规则?