我正在研究微型社交网络,我有两种不同的帐户类型,用户可以是简介或页面。修改关系协会
class User < ActiveRecord::Base
has_one :profile
has_one :page
end
现在,当我想显示用户名,我做current_user.profile.name,但如果用户是“页”,我得到一个错误,很明显。
所以,我想这个
private
def current_user
if session[:user_id]
@current_user = User.find(session[:user_id])
if @current_user.account_type == 'profile'
@current_user.profile = @current_user.page
end
end
end
但它不工作。
任何帮助都很赞赏。非常感谢!
这是一个有点混乱 - 如果帐户类型是一个配置文件,你为什么会设置自己作为一个页面?另外,我不确定'@ current_user.profile = @ current_user.page'会做什么,但'@ current_user.profile'将外键存储到'profile',所以你需要存储'如果你真的想这样做的话。 – TheDude 2013-02-12 23:16:32
我试图删除current_user的“profile”var,并且他没有,并将配置文件设置为页面。所以我不必在每个视图中的current_user.profile和current_user.page之间进行更改。相反,我只是使用current_user.profile为 – 2013-02-12 23:19:03