0
레일 4에서 나는 user
및 template
에 대한 관계를 가진 메시지 모델을 가지고 있습니다. 또한 자체 속성 인 text
이 있습니다. 여기 레일 4 모델 속성을 설정할 수 없습니다
class Message < ActiveRecord::Base
attr_accessor :text
belongs_to :user
belongs_to :template
validates :user, presence: true
validates :template, presence: true
validates :text, presence: true, if: lambda { |message| message.template.present? }
def initialize(args = {})
super
@user = args[:user]
@template = args[:template]
@text = args[:text] || (args[:template].text if args[:template].present?)
end
end
내 문제 : 나는
message = Message.create!(user: user, template: template, "hello world")
message.text
이
"hello world"
동일합니다 실행하면 (I 이미
user
및
template
있다고 가정) ,하지만 난 그가 데이터베이스에서이 레코드를 검색 할 때, 그것은
text
속성이
nil
입니다입니다, 다른 모든 속성은 정상입니다.
무엇을 제공합니까? text
이 데이터베이스에 저장되지 않는 이유는 무엇입니까?
왜 attr_accessor : text를 사용하고 있습니까? 그런 식으로 ActiveRecord 텍스트 속성을 "덮어 씁니다"그리고 모든 동작이 그 것이기 때문에 아마도 db에 기록되지 않습니다. 텍스트는 예약어 (db type'text') 일 수 있으므로주의하십시오. –