Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create an association between two rails models
    text
    copied!<p>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 entries belong to the user and the user has_many journal entries. I've created migrations that look like this:</p> <pre><code>class AddJournalEntriesToUsers &lt; ActiveRecord::Migration def change add_column :journal_entries, :user_id, :integer end end class AddIndexToJournalEntries &lt; ActiveRecord::Migration def change add_index :journal_entries, [:user_id, :created_at] end end </code></pre> <p>Here's what my User model looks like:</p> <pre><code>class User &lt; ActiveRecord::Base authenticates_with_sorcery! attr_accessible :email, :password, :password_confirmation has_many :journal_entries, dependent: :destroy validates_confirmation_of :password, :message =&gt; "should match confirmation", :if =&gt; :password validates_length_of :password, :minimum =&gt; 3, :message =&gt; "password must be at least 3 characters long", :if =&gt; :password validates_presence_of :password, :on =&gt; :create validates_presence_of :email validates_uniqueness_of :email end </code></pre> <p>And here's what my journal_entry model looks like:</p> <pre><code>class JournalEntry &lt; 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 </code></pre> <p>But when I go to create a new journal entry at <code>/journal_entries/new</code> I just a validation error that says "User can't be blank". So the user_id is not getting added to the journal entry even though I'm logged in and there is a user_id column in my db/schema.rb:</p> <pre><code>create_table "journal_entries", :force =&gt; true do |t| t.string "title" t.text "post" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false t.integer "user_id" end </code></pre> <p>Also, this is the form that I'm using on journal_entries/new to create the journal entry:</p> <pre><code>&lt;%= form_for(@journal_entry) do |f| %&gt; &lt;% if @journal_entry.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt;&lt;%= pluralize(@journal_entry.errors.count, "error") %&gt; prohibited this journal_entry from being saved:&lt;/h2&gt; &lt;ul&gt; &lt;% @journal_entry.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="field"&gt; &lt;%= f.label :title %&gt;&lt;br /&gt; &lt;%= f.text_field :title %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :post %&gt;&lt;br /&gt; &lt;%= f.text_area :post %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>What am I missing here? Do I need to add the user_id as a hidden field on the form?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload