Ruby on rails 关于rails多态性

Ruby on rails 关于rails多态性,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我有三个模型,文章,用户,批准。这是批准表 def change create_table :approvals do |t| t.references :user, foreign_key: true t.references :approvable, polymorphic: true t.timestamps end end 这是三个模型 class User < ApplicationRecord has_many :approvals

我有三个模型,文章,用户,批准。这是批准表

def change
  create_table :approvals do |t|
    t.references :user, foreign_key: true
    t.references :approvable, polymorphic: true

    t.timestamps
  end
end
这是三个模型

class User < ApplicationRecord
  has_many :approvals    
  has_many :articles, dependent: :destroy
end 

class Article < ApplicationRecord
  belongs_to :user
  has_many :approvals, as: :approvable
end


class Approval < ApplicationRecord
  belongs_to :user
  belongs_to :approvable, polymorphic: true
end
class用户
现在我想在文章展示页面中显示一个批准图像,如果当前用户已经批准了文章,这是不同的,我认为有一个非常简单的方法可以做到这一点,但我只能想出一个愚蠢的方法来解决这个问题 这样做,比如:

<% @article.approvals.each do |approval| %>
   <% if approval.user_id == current_user.id %>
     # show has approvaled image
   <% end %>
<% end %>

#这个节目有着令人赞许的形象
我觉得可能效率不高,请告诉我更好的解决方案,谢谢!:)


也许我表达得不太好,我的意思是批准是表扬还是类似的?

你可以先检查一下属于当前用户和文章的批准

因此在
controller
方法使用中(考虑到您一次只有一篇文章)

然后在视图中使用
@current\u user\u approvals
as

<% @current_user_approvals.each do |approval| %>
   # show has approval image
<% end %>

#显示已批准的图像

在这种情况下不应使用多态性。您可以使用has\u many:通过获得批准。阅读文档

迁移将是:

class CreateApprovals < ActiveRecord::Migration[5.0]
  def change
    create_table :approvals do |t|
      t.belongs_to :user
      t.belongs_to :article
    end
  end
end
class CreateApprovals
模型如下:

class User < ApplicationRecord
  has_many :articles, dependent: :destroy

  has_many :approvals
  has_many :approved_articles, through: :approvals, source: :article
end

class Article < ApplicationRecord
  belongs_to :user

  has_many :approvals
  has_many :approved_users, through: :approvals, source: :user
end

class Approval < ApplicationRecord
  belongs_to :user
  belongs_to :article
end
class用户
现在我们可以(在rails控制台中)执行以下操作:

#创建一个用户
user=user.create(名称:“user”)
#创作一篇文章
article=user.articles.create(名称:“article”)
#检查物品是否被批准
用户.已批准的文章.包括?(文章)#=>错误
#批准文章
user.approved\u文章正确

已编辑版本,因为我们确实希望在批准上使用多态性。关键是使用source_type指定多态类型(在本例中是'Article')

class用户

上面在控制台中的测试仍然有效。

是的,在我的整个项目中有很多模型都得到了批准,我只是没有显示全部代码,我想到了
include?(article)
方法,但我不知道当我使用多态时会是什么样子。@最亲爱的,在批准时使用多态是有意义的。我已经更新了答案。
class User < ApplicationRecord
  has_many :articles, dependent: :destroy

  has_many :approvals
  has_many :approved_articles, through: :approvals, source: :article
end

class Article < ApplicationRecord
  belongs_to :user

  has_many :approvals
  has_many :approved_users, through: :approvals, source: :user
end

class Approval < ApplicationRecord
  belongs_to :user
  belongs_to :article
end
# create a user
user = User.create(name: 'user')
# create an article
article = user.articles.create(name: 'article')

# check whether article is approved
user.approved_articles.include?(article) # => false
# approve the article
user.approved_articles << article
# check check whether article is approved again
user.approved_articles.include?(article) # => true
class User < ApplicationRecord
  has_many :articles, dependent: :destroy

  has_many :approvals
  has_many :approved_articles, through: :approvals, source: :approvable, source_type: 'Article'
end

class Article < ApplicationRecord
  belongs_to :user

  has_many :approvals, as: :approvable
  has_many :approved_users, through: :approvals, source: :user
end

class Approval < ApplicationRecord
  belongs_to :user
  belongs_to :approvable, polymorphic: true
end