Ruby on rails Rails有很多:通过关联问题

Ruby on rails Rails有很多:通过关联问题,ruby-on-rails,associations,has-many-through,Ruby On Rails,Associations,Has Many Through,我有一个应用程序,有三种型号:用户、相册和评论。目前,评论是我的联接表。 用户有许多评论,并且通过评论拥有许多相册。一张专辑有许多评论,并且通过评论拥有许多用户。用户属于相册和评论 我的问题是:有没有办法重新安排这些协会,使两个有很多:通过协会在不同的模式下工作?因为我当前无法呼叫album.user。它必须是“album.users”,然后显示一长串与相册完全无关的用户 我需要一个有很多,一个属于,两个有很多:通过与这三个模型的关联。这是可能的,还是我需要添加另一种模式,如流派或评论 任何建议

我有一个应用程序,有三种型号:用户、相册和评论。目前,评论是我的联接表。 用户有许多评论,并且通过评论拥有许多相册。一张专辑有许多评论,并且通过评论拥有许多用户。用户属于相册和评论

我的问题是:有没有办法重新安排这些协会,使两个有很多:通过协会在不同的模式下工作?因为我当前无法呼叫album.user。它必须是“album.users”,然后显示一长串与相册完全无关的用户

我需要一个有很多,一个属于,两个有很多:通过与这三个模型的关联。这是可能的,还是我需要添加另一种模式,如流派或评论

任何建议都很有用,我将在下面发布我的模型:

相册

classalbum:销毁
有很多:用户,通过::评论#不正确
有没有附加一个:阿凡达
接受以下内容的\u嵌套\u属性\u:reviews
验证是否存在:艺术家
验证是否存在:title
验证:艺术家的唯一标题
范围:最近的评论,->{包括(:评论)。其中(评论:{date:[(date.today-7.days)…date.moother]}
私有的
def独特的_标题_由_艺术家制作
如果相册.where('lower(title)=?AND artist=?',self.title.downcase,self.artist.)。计数>0
@错误。添加(:标题“艺术家必须唯一”)
结束
结束
结束
复习

class Review < ApplicationRecord
    belongs_to :album, optional: true
    belongs_to :user
    validates_presence_of :content
    validates :title, presence: true, uniqueness: true
    validates :date, presence: true
    accepts_nested_attributes_for :album
    scope :recent, -> { where("date(date) >= ?", Date.today - 7.days) } #scope for album
end
class Review{where(“date(date)>=?”,date.today-7.days)}
结束
使用者

需要“securerandom”
类用户<应用程序记录
有安全的密码吗
验证:电子邮件,唯一性:true,存在性:true,格式:{with:URI::MailTo::email_REGEXP}
验证:名称,唯一性:true,长度:{in:3..20},存在性:true
有很多评论吗
有很多:专辑,通过::评论
定义自授权(授权)
其中(provider:auth.provider,uid:auth.uid)。第一个用户|
user.provider=auth.provider
user.uid=auth.uid
user.name=auth.info.name
user.email=auth.info.email
user.password=SecureRandom.urlsafe\u base64
user.oauth_token=auth.credentials.token
user.oauth\u expires\u at=Time.at(auth.credentials.expires\u at)
user.save!
结束
结束
结束

您可以将
用户id
(整数)添加到
相册
表中,然后您可以跟踪相册的用户

在模型中添加

class Album < ApplicationRecord 
  has_many :reviews, :dependent => :destroy
  has_many :reviewers, through: :reviews, source: :user

  belongs_to :user
end


class User < ApplicationRecord
  has_many :reviews
  has_many :reviewed_albums, through: :reviews, source: :album

  has_many :albums
end
classalbum:销毁
有很多:审阅者,通过::审阅,来源::用户
属于:用户
结束
类用户<应用程序记录
有很多评论吗
通过::评论,来源::相册,已审阅了许多相册
有很多:专辑
结束

用户
评论
都是
评论人
(查看专辑的用户)

哦,天哪,这太有帮助了。这个过程叫什么?如果我想再次搜索它,我如何才能找到它?您可以在中阅读有关rails关联的更多信息
require 'securerandom'
class User < ApplicationRecord
    has_secure_password
    validates :email, uniqueness: true, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
    validates :name, uniqueness: true, length: {in: 3..20}, presence: true
    has_many :reviews
    has_many :albums, through: :reviews

    def self.from_omniauth(auth)
        where(provider: auth.provider, uid: auth.uid).first_or_initialize do |user|
          user.provider = auth.provider
          user.uid = auth.uid
          user.name = auth.info.name
          user.email = auth.info.email
          user.password = SecureRandom.urlsafe_base64
          user.oauth_token = auth.credentials.token
          user.oauth_expires_at = Time.at(auth.credentials.expires_at)
          user.save!
        end
      end

end
class Album < ApplicationRecord 
  has_many :reviews, :dependent => :destroy
  has_many :reviewers, through: :reviews, source: :user

  belongs_to :user
end


class User < ApplicationRecord
  has_many :reviews
  has_many :reviewed_albums, through: :reviews, source: :album

  has_many :albums
end