【问题标题】:Associate devise user with post将设计用户与帖子相关联
【发布时间】:2016-04-14 04:00:24
【问题描述】:

我正在使用设计,现在我正在尝试将用户与文章相关联。

我阅读了几篇关于 stackoverflow 的文章,但我很难理解它在我的应用程序中是如何工作的。我试图理解this question 但这似乎是黑魔法......

数据库:MongoDB(mongoid gem)

这是我所拥有的:

article_controller.rb

class ArticlesController < ApplicationController
  before_action :set_article, only: [:show, :edit, :update, :destroy]

  # GET /articles
  # GET /articles.json
  def index
    @articles = Article.all
  end

  # GET /articles/1
  # GET /articles/1.json
  def show
  end

  # GET /articles/new
  def new
    @article = Article.new
  end

  # GET /articles/1/edit
  def edit
  end

  # POST /articles
  # POST /articles.json
  def create
    @article = Article.new(article_params)

    respond_to do |format|
      if @article.save
        format.html { redirect_to @article, notice: 'Article was successfully created.' }
        format.json { render action: 'show', status: :created, location: @article }
      else
        format.html { render action: 'new' }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /articles/1
  # PATCH/PUT /articles/1.json
  def update
    respond_to do |format|
      if @article.update(article_params)
        format.html { redirect_to @article, notice: 'Article was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /articles/1
  # DELETE /articles/1.json
  def destroy
    @article.destroy
    respond_to do |format|
      format.html { redirect_to articles_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_article
      @article = Article.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def article_params
      params.require(:article).permit(:title, :content)
    end
end

article.rb

class Article
  include Mongoid::Document
  belongs_to :user

  field :title, type: String
  field :content, type: String
  field :user, type: String

  default_scope -> { order(created_at: :desc) }
end

用户.rb

class User
  include Mongoid::Document
  has_many :articles
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  # ... devise configurations
end

我对如何正确地做到这一点了解不足。

【问题讨论】:

    标签: ruby-on-rails ruby mongodb devise


    【解决方案1】:

    首先在文章模型中,你必须删除这行

    field :user, type: String
    

    这是错误的,它会与 Mongoid 生成的获取用户关系的方法冲突。

    其次,您必须正确构造文章对象... 这可以通过以下任何代码来完成

    # METHOD 1
    @article = Article.create!(
      title: article_params[:article][:title],
      content: article_params[:article][:content],
      user: current_user
      # or user_id: current_user.id
    )
    
    # METHOD 2
    @article = current_user.articles.create!(article_params)
    
    # METHOD 3
    # this is mentioned in other answer as well ( but added also here for completeness )
    @article = Article.new(article_params)
    @article.user = current_user
    @article.save!
    

    并详细说明这是如何工作的(关于您提到的问题),我将解释每种方法的工作原理......

    方法一

    在这种方法中,您正在创建一个对象并手动设置它的所有属性...这意味着您将使用正确的值覆盖 :user:user_id,以便将对象正确序列化到 DB (无论是 mongoDB 还是 Postgres 或其他)

    方法二

    在这种方法中,您将获取用户模型本身并使用关系辅助方法创建文章...这将自动计算文章的user_id 属性并使用其余关系正确设置它。 . (因为用户有很多文章...任何user.articles.create! 都会抓住关系user_id

    方法3

    在这种方法中,您使用 @article = Article.new(article_params) 和所需属性的子集创建一个 ruby​​ 对象(不是数据库记录...)...并且您不会保留该数据库(还)。 .. 然后您自己手动设置article &lt;-&gt; user 关系... 最后,您使用.save.save! 将该记录保存到数据库

    【讨论】:

    • 我使用了方法 2 - 感谢您的解释。它比我想象的要容易。现在,我如何查看该用户是否实际分配给了这篇文章?我使用了&lt;%= @article.user %&gt;,它给了我这个:#&lt;User:0x007fdcd340d8c0&gt;
    • 我认为这是正确的......进一步调查......你可以显示@article.user.id@article.user.email或任何你想要的......@article.user返回整个用户对象(你可以用来做任何你想做的事情)......如果你熟悉 ORM 的概念,那么你应该没问题
    • 是的,是的——我的大脑今天关机了。因此,在我的情况下,解决方案是&lt;%= @article.user.username %&gt;,它将查找文章/用户/用户名。非常感谢@mad_raz
    【解决方案2】:

    在您的文章控制器中,与链接答案中的内容相同:

    @article.user = current_user

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      相关资源
      最近更新 更多