How to create an association between two rails models

后端 未结 5 1162
南笙
南笙 2021-01-16 12:07

This is a newbie question, but I\'m still learning how to create an association between two models in rails. I have a user model and a journal_entry model. The journal entri

5条回答
  •  没有蜡笔的小新
    2021-01-16 12:49

    journal_entry model should look like

    class JournalEntry < ActiveRecord::Base
      attr_accessible :post, :title, :user_id
      belongs_to :user
      validates :user_id, presence: true
      default_scope order: 'journal_entries.created_at DESC'
    end
    

    This should work!

提交回复
热议问题