【问题标题】:Active Record Association one to manyActive Record 关联一对多
【发布时间】:2013-12-25 02:41:54
【问题描述】:

我对当前的 Active Record 关联有疑问。

我正在制作一个基于问题的评论系统(非常类似于 stackoverflow)。

我正在查看关联,一个用户有很多 cmets,一个评论属于一个用户。

现在,当我进行实际评论时,用户 ID 应该包含在我的评论的 user_id 字段中,因为我正在建立关联,对吗?

这是我的架构:

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

这是我的 Active Record 关联:

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

评论.rb:

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

我的最终目标是让我的 cmets 架构在发表新评论时注册 user_id,从而将评论与用户相关联。

现在,我的 user_id 是“nil”

**这是 Backbone 前端上的 rails 后端

【问题讨论】:

  • 你还没有分享user_idnil的上下文(例如代码和输出),所以这个问题很难回答。
  • 抱歉,彼得,这是您要查找的内容吗,这是来自我的 rails 控制台的正常查询,这是 Comment.last 返回的内容 #
  • 好的,这解释了您是如何查看它的,但仍然没有解决您如何创建它的问题。我的回答有帮助吗?

标签: mysql ruby-on-rails rails-activerecord


【解决方案1】:

不,关联记录的user_id 字段只有在您通过关联创建时才会“自动”填写,如下所示:

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

请参阅http://guides.rubyonrails.org/association_basics.html#has-many-association-reference 了解其他选项。当然,您也可以手动设置user_id

【讨论】:

    猜你喜欢
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2022-01-26
    • 1970-01-01
    相关资源
    最近更新 更多