これは初心者の質問ですが、レールで 2 つのモデル間の関連付けを作成する方法をまだ学んでいます。ユーザー モデルと journal_entry モデルがあります。ジャーナル エントリはユーザーに属し、ユーザーには多数のジャーナル エントリがあります。次のような移行を作成しました。
class AddJournalEntriesToUsers < ActiveRecord::Migration
  def change    
    add_column :journal_entries, :user_id, :integer
  end
end
class AddIndexToJournalEntries < ActiveRecord::Migration
  def change
    add_index :journal_entries, [:user_id, :created_at]
  end
end
私の User モデルは次のようになります。
class User < ActiveRecord::Base
  authenticates_with_sorcery!
  attr_accessible :email, :password, :password_confirmation
  has_many :journal_entries, dependent: :destroy
  validates_confirmation_of :password, :message => "should match confirmation", :if => :password
  validates_length_of :password, :minimum => 3, :message => "password must be at least 3 characters long", :if => :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email
  validates_uniqueness_of :email
end
そして、これが私のjournal_entryモデルがどのように見えるかです:
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
しかし、新しい日誌エントリを作成しようとすると、 /journal_entries/new「ユーザーを空白にすることはできません」という検証エラーが表示されます。そのため、ログインしていて、db/schema.rb に user_id 列があるにもかかわらず、user_id がジャーナル エントリに追加されません。
create_table "journal_entries", :force => true do |t|
    t.string   "title"
    t.text     "post"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.integer  "user_id"
  end
また、これはジャーナル エントリを作成するために、journal_entries/new で使用しているフォームです。
<%= form_for(@journal_entry) do |f| %>
  <% if @journal_entry.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@journal_entry.errors.count, "error") %> prohibited this journal_entry from being saved:</h2>
      <ul>
      <% @journal_entry.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :post %><br />
    <%= f.text_area :post %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
ここで何が欠けていますか?フォームの非表示フィールドとして user_id を追加する必要がありますか?