How to create a connection between two rails

This is a newbie question, but I'm still involved in creating the connection between the two models in rails. I have a user model and journal_entry model. The journal entries belong to the user, and the user has journal entries for the journal. I created migrations that look like this:

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 

This is what my User model looks like:

 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 

And this is what my journal_entry model looks 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 

But when I move on to creating a new journal entry in /journal_entries/new , I just check for the error that says: "User cannot be empty." This way, user_id is not added to the log entry, even if I am logged in and there is a user_id column in my db / schema.rb:

 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 

In addition, this is the form I use to create journal_entries / new journal entries:

 <%= 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 %> 

What am I missing here? Do I need to add user_id as a hidden field in the form?

+1
ruby ruby-on-rails rails-models
Jul 6 '12 at 1:26
source share
5 answers

I'm sure you will forget something like

 def create @journal_entry = @user.journal_entries.build(params[:journal_entry]) # @journal_entry = current_user.journal_entries.build(params[:journal_entry]) if @journal_entry.save .. 
+3
Jul 06 '12 at 22:26
source share
Model

journal_entry 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 

That should work!

+2
Jul 06 '12 at 3:40
source share

You need to add user_id to your attr_accessible call, if you look at your logs, this probably warns you that it cannot assign it a mass.

0
Jul 06 2018-12-12T00:
source share

Ok, so I got this working by adding the user to the create action in my_entries_controller.rb log. Here's the code I used, but is this a β€œrails” way?

 def create @user = current_user @journal_entry = @user.journal_entries.build(params[:journal_entry]) if @journal_entry.save flash[:success] = "Journal entry created!" end end 
0
Jul 07 2018-12-12T00:
source share

this time you are fine. You have added a user association to the log model, which loads the user into the controller before displaying it in the view. You need the hidden fields in your form that you added since you are using a stateless protocol. In the update / create action, double-check that the user publication is being used and saved by the user.

0
Jul 07 '12 at 16:17
source share



All Articles