0
votes

I have an app with three models - User, Album, and Review. Currently, Reviews are my join table. A User has many Reviews and has many Albums through Reviews. An Album has many Reviews and has many Users through Reviews. A User belongs to an Album and a Review.

My question is: Is there a way to re-arrange these associations to make two has_many :through associations work on a different model? Because I cannot currently call album.user. It has to be 'album.users' and then displays a long list of users totally unrelated to the album.

I need to have One has_many, One belongs_to and Two has_many :through associations with these three models. Is that even possible, or do I need to add another model like Genre or Comment?

Any suggestions are helpful, I'll post my models below:

Album

class Album < ApplicationRecord 
    has_many :reviews, :dependent => :destroy
    has_many :users, through: :reviews # incorrect
    has_one_attached :avatar
    accepts_nested_attributes_for :reviews
    validates_presence_of :artist
    validates_presence_of :title
    validate :unique_title_by_artist
    scope :with_recent_reviews, -> { includes(:reviews).where(reviews: { date: [(Date.today - 7.days)..Date.tomorrow] }) } 

    private

    def unique_title_by_artist
        if Album.where('lower(title) = ? AND artist = ?', self.title.downcase, self.artist).count > 0
          @errors.add(:title, "must be unique to artist")
        end
    end

end

Review

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

User

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
1

1 Answers

1
votes

you can add user_id(integer) to albums table, then you can track album's user

in the models add

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

users through reviews are reviewers(users who reviewed the album)