댓글 기능 추가하기 — 모델 연관관계 완전 정복
·
Ruby On Rails
게시판, 블로그, 커뮤니티… 어디서든 빠지지 않는 기능이 바로 댓글이죠.이번 글에서는 Post와 Comment 사이의 관계를 설정하고, 댓글 작성/조회/삭제까지 구현하면서 Rails의 모델 연관관계를 완전히 이해해볼 거예요.🧱 1. 모델 관계 설계: Post has_many CommentsPost가 여러 개의 Comment를 가지는 구조입니다.$ bin/rails g model Comment body:text post:referencespost:references는 post_id 컬럼을 자동으로 추가하고, 외래키 관계를 설정합니다.# db/migrate/xxxxx_create_comments.rbt.text :bodyt.references :post, null: false, foreign_key: tr..