简体   繁体   中英

Active Record Association one to many

I have a question with my current Active Record Associations.

I am making a commenting system based on a question (very similar to stackoverflow).

I am looking at the association and a User has many comments, and a Comment belongs_to a User.

Now, when I make an actual comment, the user_id should be included with in my comment's user_id field since I am making the association, correct?

Here is my schema:

create_table "users", force: true do |t|
    t.string   "email",                              default: "", null: false
    t.string   "encrypted_password",                 default: "", null: false
    t.integer  "question_id",            limit: 255
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",                      default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "provider"
    t.string   "uid"
    t.string   "name"
  end

create_table "comments", force: true do |t|
    t.integer  "user_id"  **THIS IS WHERE I AM EXPECTING A JOIN AND WHEN A USER MAKES A COMMENT TO HAVE THE USER_ID NUMBER STORED HERE
    t.integer  "question_id"
    t.text     "comment"
    t.integer  "upvotes"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Here are my Active Record Associations:

USER.RB:

class User < ActiveRecord::Base
  has_many :questions
  has_many :comments
  has_many :votes

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :omniauthable,
         :recoverable, :rememberable, :trackable, :validatable


      def self.from_omniauth(auth)
        where(auth.slice(:provider, :uid)).first_or_create do |user|
          user.provider = auth.provider
          user.uid = auth.uid
          # user.username = auth.info.nickname
          user.email = auth.info.email
          user.name = auth.info.name
        end
      end

      def self.new_with_session(params, session)
        if session["devise.user_attributes"]
          new(session["devise.user_attributes"], without_protection: true) do |user|
            user.attributes = params
            user.valid?
          end
        else
          super
        end
      end

      def password_required?
        super && provider.blank?
      end

      def update_with_password(params, *options)
        if encrypted_password.blank?
          update_attributes(params, *options)
        else
          super
        end
      end
end

Comment.rb:

class Comment < ActiveRecord::Base
  belongs_to :question
  belongs_to :user
  has_many :votes
end

My end goal is to have my comments schema to register the user_id when a new comment is made, associating the comment to the user.

Right now, my user_id is "nil"

**This is a rails backend on a Backbone frontend

No, the user_id field of an associated record will only be filled in "automatically" if you create it through the association, as in:

user.comments << Comment.create(...)

See http://guides.rubyonrails.org/association_basics.html#has-many-association-reference for other options. You can, of course, set user_id manually as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM