Ruby on rails Rails 5 API-使用关联模型中的值在has_one一侧设置默认值

Ruby on rails Rails 5 API-使用关联模型中的值在has_one一侧设置默认值,ruby-on-rails,heredoc,Ruby On Rails,Heredoc,似乎是有关设置模型默认值的堆栈溢出问题中投票最多/最相关的问题。尽管探索了许多方法,但没有一种方法涵盖这一问题 请注意,这是一个API-因此提醒的默认文本必须由API提供,因此我将其存储在数据库中。有些用户希望使用该默认值,有些用户将对其进行更新 这是我的代码,但感觉不对,因为我必须在“父级”(用户模型)中设置一个默认值(对于:body) class用户

似乎是有关设置模型默认值的堆栈溢出问题中投票最多/最相关的问题。尽管探索了许多方法,但没有一种方法涵盖这一问题

请注意,这是一个API-因此提醒的默认文本必须由API提供,因此我将其存储在数据库中。有些用户希望使用该默认值,有些用户将对其进行更新

这是我的代码,但感觉不对,因为我必须在“父级”(
用户
模型)中设置一个默认值(对于
:body

class用户我认为,这是一个棘手的事实,可能是一个迹象,表明你可能想重新设计这一点。特别是,看起来您正在混合模型逻辑和视图逻辑

您可以通过将实际创建字符串的逻辑移到更靠近要渲染字符串的位置(ERB文件、电子邮件模板等)来解决此问题。然后,当您传入为其生成字符串的
提醒
实例时,调用
@rementer.user.name
的代码将起作用,因为
@rementer
。user
关联不是空的


我认为这是一个棘手的事实,可能表明你 可能需要重新设计一下。特别是,它看起来像 您正在混合模型逻辑和视图逻辑

这是@Adam Berman的中肯观察。所以它被重新设计了-在@MrYoshiji评论的帮助下,谢谢大家

我认为以下是一个更好、更易于维护的解决方案,尤其是当应用程序确实需要能够提供这些默认英文文本的实际翻译时——因此我还将为这两个属性创建
自定义问候语
自定义主题

class User < ActiveRecord::Base    
  # Relationships
  belongs_to :account
  has_one :reminder    
end

class Reminder < ApplicationRecord
  # Relationships
  belongs_to :user

  # Default attributes
  attribute :subject, :string, default: "your checklist reminder!"
  attribute :greeting, :string, default: "Hi"

  def body=(value)
    self.custom_body = value
  end

  def body
    custom_body.presence || I18n.t('reminder.default_body', name: self.user.name)
  end
end
(对于yml中的多行文字帮助很大)


(帮助理解getter/setter-请参阅@pascal betz关于存储价格的答案)

感谢您的快速回答,但不幸的是,我忘了提到这是一个API,用户可以忽略此默认消息(但大多数情况下不会,因此API需要提供此默认值,因此必须为每个用户将其存储在数据库中)。现在将此添加到问题中。如果值为空,您可以使用I18n作为默认文本,并使您的模型回退到这些I18n翻译。类似于
自定义_body
(文本值以DB表示)然后定义getter
def body;custom_body.presence|I18n.t('defaults.body',username:user.name);end
是新行的缩写)@MrYoshiji这是一个有趣的想法,我没有想到。似乎是一个更好的解决方案。我会试试。
class User < ActiveRecord::Base    
  # Relationships
  belongs_to :account
  has_one :reminder    
end

class Reminder < ApplicationRecord
  # Relationships
  belongs_to :user

  # Default attributes
  attribute :is_follow_up, :boolean, default: true
  attribute :subject, :string, default: "your checklist reminder!"
  attribute :greeting, :string, default: "Hi"
  attribute :body, :string, default:
    <<~HEREDOC
      This is a quick reminder about the outstanding items still on your checklist.
      Thank you,
      #{self.user.name}
    HEREDOC
end
undefined method `user' for #<Class:0x007fa59d4d0f90>
class User < ActiveRecord::Base    
  # Relationships
  belongs_to :account
  has_one :reminder    
end

class Reminder < ApplicationRecord
  # Relationships
  belongs_to :user

  # Default attributes
  attribute :subject, :string, default: "your checklist reminder!"
  attribute :greeting, :string, default: "Hi"

  def body=(value)
    self.custom_body = value
  end

  def body
    custom_body.presence || I18n.t('reminder.default_body', name: self.user.name)
  end
end
  reminder:
    default_body: "\nThis is a quick reminder about the outstanding items still on your checklist.\
      \nPlease click the button below to review the items we're still waiting on.\
      \nThank you,\
      \n%{name}"