我有一个与组织模型相关的成员模型:1个组织有许多成员。在下面的控制器方法中,我尝试在第3行中找到一个组织,将其状态设置为在线激活。
但是,在运行测试时,我收到错误消息 def edit
member = Member.find_by(email: params[:email])
organization = Organization.find_by(params[:member.organization_id])
if member && !member.activated? && member.authenticated?(:activation, params[:id])
member.activate
log_in("member", member)
flash[:success] = "Account activated! You can now update your profile."
redirect_to member
organization.update_attributes(activated: true) unless organization.activated?
elsif user && !user.activated? && user.authenticated?(:activation, params[:id])
user.activate
log_in("user", user)
flash[:success] = "Account activated! You can now update your profile."
redirect_to user
else
flash[:danger] = "Invalid activation link"
redirect_to root_url
end
end
。我不认为那个成员是零。可能导致这种情况的任何想法?
^[^\.]*[a-zA-Z]+$
答案 0 :(得分:0)
member = Member.find_by(email: params[:email])
organization = Organization.find_by(params[:member.organization_id])
你已经拥有member
,ActiveRecord::Associations
会做出魔术:
member = Member.find_by(email: params[:email])
if member
organization = member.organization
else
#whatever
end
答案 1 :(得分:0)
你在这条线上有一个错字。而不是打电话
organization = Organization.find_by(params[:member.organization_id])
您需要直接致电member.oranization_id
organization = Organization.find_by(member.organization_id)
然而,使用活动记录关联功能
可以进一步简化这一点organization = member.organization