Ruby on rails 将设计用户与post关联
我正在使用Desive,现在我正在尝试将用户与文章关联起来 我读了一些关于stackoverflow的帖子,但我很难理解它在我的应用程序中是如何工作的。我试着去理解,但这看起来像是黑魔法 数据库:MongoDB(mongoid gem) 以下是我所拥有的: article_controller.rbRuby on rails 将设计用户与post关联,ruby-on-rails,ruby,mongodb,devise,Ruby On Rails,Ruby,Mongodb,Devise,我正在使用Desive,现在我正在尝试将用户与文章关联起来 我读了一些关于stackoverflow的帖子,但我很难理解它在我的应用程序中是如何工作的。我试着去理解,但这看起来像是黑魔法 数据库:MongoDB(mongoid gem) 以下是我所拥有的: article_controller.rb class ArticlesController < ApplicationController before_action :set_article, only: [:show, :ed
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
user.rb
class User
include Mongoid::Document
has_many :articles
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# ... devise configurations
end
我对如何正确地做这件事知之甚少。在您的文章中,与链接答案中的内容相同:
@article.user=文章控制器中的当前用户,与链接答案中的内容相同:
@article.user=current_user
首先在文章模型中,必须删除此行
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!
为了详细说明这是如何工作的(关于你提到的问题),我将解释每种方法是如何工作的
方法1
在这种方法中,您将创建一个对象并手动设置其所有属性。。。这意味着您正在使用正确的值覆盖:user
或:user_id
,它们希望将对象正确序列化到DB(无论是mongoDB还是Postgres或其他)
方法2
在这种方法中,您将获取用户模型本身,并使用关系助手方法创建文章。。。这将自动计算文章的user\u id
属性,并将其与其余关系正确设置。。。(由于用户有许多文章…任何user.articles.create!
都将获取关系user\u id
方法3
在这种方法中,您可以使用@article=article.new(article\u params)
和所需属性的子集创建一个ruby对象(还不是数据库记录),并且您不需要将数据库(还)持久化。。。
然后您自己手动设置文章用户
关系。。。
最后,使用.save
或.save!
将记录保存到数据库中首先在文章模型中,必须删除这一行
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!
为了详细说明这是如何工作的(关于你提到的问题),我将解释每种方法是如何工作的
方法1
在这种方法中,您正在创建一个对象并手动设置它的所有属性……这意味着您正在使用正确的值覆盖:user
或:user\u id
,它们希望将对象正确序列化到DB(无论是mongoDB还是Postgres或其他)
方法2
在这种方法中,您将获取用户模型本身,并使用Relationship helper方法创建文章…该方法将自动计算文章的user\u id
属性,并将其与其余关系正确设置。。。(由于用户有许多文章…任何user.articles.create!
都将获取关系user\u id
方法3
在这种方法中,您可以使用@article=article.new(article\u params)
和所需属性的子集创建一个ruby对象(还不是数据库记录),并且您不需要将数据库(还)持久化。。。
然后您自己手动设置文章用户
关系。。。
最后,您使用.save
或.save!
将该记录持久化到数据库中。我使用了方法2-谢谢您的解释。这比我原来认为的更简单。现在,我怎么看这个用户是否真的被分配到了本文中?我使用了
,它给了我以下信息:
,这是正确的,我想您可以显示@article.user.id
或@article.user.email
或任何您想要的…@article.user
返回整个用户对象(您可以使用它执行任何您想要执行的操作)…如果你熟悉ORMs的概念,那么你应该很好是的,是的-我的大脑今天被关闭了。所以在我的情况下,解决这个问题的方法是
,它将查找文章/用户/用户名。非常感谢@mad_razI使用方法2-感谢你解释它。这比我想象的更容易。现在,我怎么看这ser实际上被分配给了这篇文章?我使用了
,它给了我以下信息:
,我认为这是正确的…为了进一步调查…你可以显示@article.user.id
或@article.user.email
或任何你想要的…@article.user
返回整个用户对象(你可以用它做你想做的任何事情)…如果你熟悉ORMs的概念,那么你应该很好是的,是的-我的大脑今天被关闭了。所以解决这个问题的方法是
,它将查找文章/用户/用户名。非常感谢@mad_raz