Я хочу добавить связь между существующим постом и автором.
Я пытался изменить атрибут created_by
, но он недоступен из объекта.
def set_author
if (@post.created_by.empty? && @post.author_code.present?)
if @post.author_code == params[:author_code]
@post.created_by = current_user
else
raise(ExceptionHandler::InvalidAuthorCode, Message.invalid_author_code)
end
else
raise(ExceptionHandler::DisallowedAction, Message.action_not_allowed)
end
end
Это не работает, потому что нет метода @post.created_by
, даже если он присутствует в БД.
Опубликовать модель из schema.rb
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "content"
t.boolean "accepted", default: false
t.string "created_by"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "author_code"
end
Edit:
часть post.rb
belongs_to :user, optional: true, foreign_key: :created_by
часть user.rb
has_many :confessions, foreign_key: :created_by