我有一个用户和用户详细信息模型,它们处于has-one <->属于关系。最初,当用户注册(创建)时,他的详细信息不会被填充。
我希望在用户登录时,如果 未填充他的详细信息以显示详细信息表单并“强制”他完成他的个人资料。
因此,在应用程序控制器中,我添加了以下代码:
before_filter :is_profile_complete
helper_method :current_user
private
def current_user
@current_user ||= SecurityUser.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
end
def is_profile_complete
if current_user
unless current_user.is_profile_completed
redirect_to security_users_detail_path
end
end
end
所以,一般来说,如果用户登录,is_profile_complete 方法会调用 is_profile_complete 模型方法,看起来:
def is_profile_completed
is_completed = true
self.security_users_detail.attributes.each do |field_name, field_value|
if field_value.blank? || field_value.empty?
is_completed = false
break
end
end
is_completed
end
所以,我想检查是否有任何详细信息字段为空 - 如果是,则配置文件未完成。
不幸的是,上面的模型方法会产生以下错误:
ActiveRecord::StatementInvalid at /
ActiveRecord::JDBCError: DB2 SQL 错误: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=SECURITY_USERS_DETAILS.SECURITY_USER_ID, DRIVER=3.65.77: SELECT security_users_details.* FROM security_users_details where security_users_details.security_user_id = 301 只取第一行
就我尝试调试而言,问题是由以下语句引起的:
self.security_users_detail.attributes
有谁知道为什么会发生这种情况?
编辑:
详细信息表字段下方的屏幕截图:
如您所见,没有引用用户表的列。这可能是导致问题的原因吗?这是否意味着如果我使用任何关联,我总是应该为参考创建额外的列?