我试图找出如何在我的用户模型中编写方法,该方法在一行中显示用户的全名和用户所在组织的标题。
在我的用户模型中,我有一个名为full_name的方法,它可以将名字和姓氏属性一起添加。那部分有效。
现在,我尝试编写名为' formal_title'的方法,将用户的全名添加到organisation.title。
我有用户和组织的模型。协会是:
用户
belongs_to :organisation
组织
has_many :users
我的组织表有一个名为:title。
的属性我在用户模型中编写方法的尝试是:
def full_name
if first_name.present?
[*first_name.capitalize, last_name.capitalize].join(" ")
else
test_full_name
end
end
def organisation_title
Organisation.title.where(organisation.id == self.organisation_id).titleize
end
def formal_title
[*self.full_name, self.organisation_title].join(",")
end
当我尝试这个时,我在控制台中收到错误消息:
NoMethodError: undefined method `formal_title' for #<User:0x007fea3495fe90>
此错误消息对我没有意义,因为我在其上测试此用户的用户具有要测试的名字,姓氏和组织ID。用户表中的organisation_id引用了一个具有标题的组织,因此我不知道为什么这个方法的任何部分可能会丢失,但也不知道控制台对该方法的了解程度。< / p>
任何人都可以看到我做错了吗?
整个用户模型具有:
class User < ApplicationRecord
rolify strict: true # strict means you get true only on a role that you manually add
attr_accessor :current_role
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :lockable,
:omniauthable, :omniauth_providers => [ :linkedin, :twitter, :orcid ]
# --------------- associations
belongs_to :organisation, optional: true
has_one :device
has_many :identities, dependent: :destroy
has_one :profile, dependent: :destroy
has_one :setting, dependent: :destroy
has_one :org_request, dependent: :destroy
has_many :proposals, dependent: :destroy
# Conversations
has_many :authored_conversations, class_name: 'Conversation', foreign_key: 'author_id'
has_many :received_conversations, class_name: 'Conversation', foreign_key: 'received_id'
has_many :personal_messages, dependent: :destroy
# End Conversations
# teams
has_many :teams, foreign_key: "team_mate_id"
has_many :team_projects, through: :teams, source: :proposal
has_many :received_invitations, :class_name => "TeamInvitation", :foreign_key => 'recipient_id'
has_many :sent_invitations, :class_name => "TeamInvitation", :foreign_key => 'sender_id'
# --------------- scopes
# --------------- validations
validates :first_name, presence: { message: 'First name cannot be blank.' }
validates :last_name, presence: { message: 'Last name cannot be blank.' }
validates :email, format: { with: EMAIL_REGEX }
validates :email, uniqueness: { message: 'This email is already associated with an account. Try logging in.'}
# validates_format_of :first_name, with: /a-zA-Z/
# validates_format_of :last_name, with: /a-zA-Z/
# validates_length_of :password, within: 8..128
# --------------- class methods
def full_name
if first_name.present?
[*first_name.capitalize, last_name.capitalize].join(" ")
else
"test_full_name"
end
end
def organisation_title
# you don't need to do `Organisation.where...` here because you have defined the `belongs_to` association for `:organisation`. So, it will directly give you the `Organisation` object.
organisation.title.titleize
end
def formal_title
[*self.full_name, self.organisation_title].join(",")
end
# --------------- instance methods
def online?
true
# false
# !Redis.new.get("user_#{self.id}_online").nil?
end
end
答案 0 :(得分:1)
请尝试更改此代码,并告诉我它的内容:
def full_name
if first_name.present?
[first_name.capitalize, last_name.capitalize].join(" ")
else
test_full_name # will work if test_full_name is defined inside this class
end
end
def organisation_title
# you don't need to do `Organisation.where...` here because you have defined the `belongs_to` association for `:organisation`. So, it will directly give you the `Organisation` object.
organisation.title.titleize
end
def formal_title
[self.full_name, self.organisation_title].join(",")
end
答案 1 :(得分:1)
我每次都保存更改。然后我在控制台中
reload!
。
还不够。您应该重新启动控制台或重新查询用户。
u = User.find(u.id)
reload!
不会影响已加载的对象,因此您拥有的用户不会看到新方法。