【发布时间】:2013-01-08 15:48:58
【问题描述】:
我正在尝试让尚未填写个人资料的用户重定向到编辑页面。
我有两个表 - 用户(设计句柄)和配置文件。
Profiles 有一个名为 profile_complete 的行
在我的用户模型中,我试图定义一个名为 profile_complete 的方法,该方法采用 profiles_complete 的布尔值并将其传回。
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
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :profiles, :dependent => :destroy
def profile_complete?
self.profile.profile_complete == true
end
end
但似乎无法弄清楚 profile_complete 方法中的行是什么。我已经让其他位在正确的位置工作,但无法通过任何帮助获得这个变量?干杯:)
【问题讨论】:
-
在几乎每个实例中,将某些内容与
true进行比较是没有意义的。您应该按原样返回。 -
您的用户应该有
has_one :profile吗?那么你应该可以做到profile && profile.profile_complete -
他们可以有很多个人资料 :) @bullfrog
-
是否每个与用户相关的配置文件都必须完整才能使该方法返回 true ?如果是这样,那么
self.profiles.all? {|profile| profile.complete} -
@bullfrog,如果给定用户没有关联的个人资料,它将返回 true。例如:[].all? {假} #=> 真。你还需要像 !self.profiles.empty?
标签: ruby-on-rails ruby rails-models