如何根据另一个字段更改模型中的字段?

发布于 2025-02-01 01:32:56 字数 361 浏览 4 评论 0原文

问候其他开发商。 我有一个模型候选者。RB,我想使用的2列(uperf_code and conticate_type)。 如果cantidate_type =“ chapter”,我想要gupt_code =“ chp”。 我正在尝试使用after_save做到这一点,但这是用cantixed_type =“ chapter”创建所有新的候选人。 我认为我使用的条件是错误的。请指导我

在候选人中

After_save : chapter_offer

def chapter_offer
 If self.candidate_type =  "chapter"
    self.offer_code = "CHP"  
 end
end

Greetings fellow developers.
I have a model candidate.rb and there are 2 columns(offer_code and candidate_type) that i want to work with.
I want the offer_code = "CHP" if the candidate_type = "chapter".
I am trying to do this with after_save but this is creating all the new candidates with candidate_type = "chapter".
I think the condition that I have used is wrong. Please guide me

In candidate.rb

After_save : chapter_offer

def chapter_offer
 If self.candidate_type =  "chapter"
    self.offer_code = "CHP"  
 end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空宴 2025-02-08 01:32:56

您正在使用=进行比较,而不是==

,如果您做这样的事情会更好

before_save :chapter_offer, if: -> { candidate_type == 'chapter' }

def chapter_offer
  offer_code = 'CHP'
end

You are using = for comparison instead of ==

And it will be better if you did something like this

before_save :chapter_offer, if: -> { candidate_type == 'chapter' }

def chapter_offer
  offer_code = 'CHP'
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文