Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9007143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:36:29+00:00 2026-06-16T01:36:29+00:00

I am attempting to create a journal where a logged in user’s journal_entries are

  • 0

I am attempting to create a journal where a logged in user’s journal_entries are listed when the index view is called.

I have installed the Devise gem for the user sign up, log in/out authentication.

I have created a model, JournalEntries, which includes a date, string and text fields.

After creating the database, rake db:create and then migrating, rake db:migrate – I attempted to list a users journal entries in the journal_entries/index view. The default view that was derived via scaffolding lists ALL USERS journal_entries. This doesn’t make for a great journal – where you can see the entries of all other users.

In my research I’ve come to realize that I didn’t have a field in the journal_entries table which referenced the users table.

I create a migration show below:

class AddForeignKeyToJournalEntries < ActiveRecord::Migration
  def up
    change_table :journal_entries do |t|
        t.references :user
    end
    #add a foreign key
    execute <<-SQL
        ALTER TABLE journal_entries
            ADD CONSTRAINT fk_journal_entries_users
            FOREIGN KEY (user_id)
            REFERENCES users(id)
    SQL
  end

  def down
    execute <<-SQL
        ALTER TABLE journal_entries
            DROP FOREIGN KEY fk_journal_entries_users
    SQL
  end

end

(I also changed the users.rb and journal_entries.rb models to include belongs_to and has_many association – at first I thought this is all I had to do, and somehow the database would pick this up, but it didn’t…)

This successfully added foreign key – user_id to the journal_entries table, and I thought I was in the clear.

What is happening now is when a journal_entry is created – the user_id column is blank – no info is being populated there….? I have verified this by logging into Postgres on my local machine.

Any help would be greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:36:30+00:00Added an answer on June 16, 2026 at 1:36 am

    The reason the user_id column is blank in the journal_entry table is because you need to add the current user’s id when a new journal entry is created.

    In your JournalEntries Controller you’ll need to add this to your create action:

    @journal_entry.user_id = current_user.id

    or, @journal_entry.user = current_user

    Then, in your JournalEntries Controller index action:

    Instead of @journal_entries = JournalEntry.all you can display only the current logged in user’s entries with @journal_entries = current_user.journal_entries.

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While attempting to create a replacement tablespace for USER, I accidentally created a datafile
I'm attempting to create a CustomControl which will have various properties affected by an
I'm attempting to create a grid style view (similar to NSCollectionView ), except using
I am attempting to create my own view in android which is simply an
I am attempting to create a system to validate a user's username and password.
I am attempting to create my first OS-level GUI using wxPython. I have the
When attempting to create a form with UITextFields, it appears that cellForRowAtIndexPath: gets called
I am attempting to create a custom tableview where I have a list of
Im attempting to create a new operator :? on lists, which operates the same
I attempting to create an 800x500 div with four overlapping divs inside the container

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.