Ich bin sehr neu in Ruby on Rails und ich brauche Hilfe, herauszufinden, wie eine bestehende DB-Abfrage zu ändern. Ich verwende alte Versionen, die ich kann nicht aktualisiert werden: Rubin 2.2.3p173 und Rails 4.0.2.
Ich möchte die bestehenden Abfrageergebnisse filtern Datensätze zu entfernen, die haben noch keine Videos. Die Modellhierarchie ich denke, ist: Artist, AlbumGroup, Album, Titel, Video.
Zur Klarstellung: Ich mag Künstler mit mindestens 1 Video, aus dem Modell Verein Künstler-> AlbumGroup-> Fotoalben-> Tracks-> Videos (nicht Künstler-> Videos).
Die vorhandene Abfrage wird in Modell Künstler enthalten:
require_dependency tagging
require_dependency similar
class Artist < ActiveRecord::Base
has_many :images, dependent: :destroy
has_many :taggings, dependent: :destroy
has_many :artforms, through: :taggings
has_many :similars, foreign_key: similar_id, dependent: :destroy
has_many :similar_artists, through: :similars, source: :similar_to
has_many :reverse_similars, foreign_key: similar_to_id, class_name: Similar, dependent: :destroy
has_many :similar_to_artists, through: :reverse_similars, source: :similar
has_many :memberships, foreign_key: member_id, dependent: :destroy
has_many :groups, through: :memberships, source: :membership
has_many :group_members, foreign_key: membership_id, class_name: Membership, dependent: :destroy
has_many :members, through: :group_members, source: :member
has_many :users, through: :followed_artists
has_many :videos, dependent: :destroy
has_many :audios, dependent: :destroy
has_many :metrics, through: :audios
has_many :releases, foreign_key: 'artist_id', class_name: AlbumGroup
has_many :albums
has_many :collaborations
has_many :album_groups, through: :collaborations
mount_uploader :mugshot, MugshotUploader
include PgSearch
pg_search_scope :for_name,
against: :name,
using: { tsearch: {threshold: '1', dictionary: 'simple', tsvector_column: 'tsv_name', prefix: true, normalization: 2}},
ranked_by: (artists.popularity / 50 * :tsearch) + :tsearch
end
Ich möchte so etwas wie die folgenden hinzufügen, um die Datensätze zu filtern aus, die haben noch keine Videos: (auf die Abfrage):
if: artist.releases.albums.tracks.videos.count > 1
Oder der Künstler Modell vielleicht ?:
scope :valid, -> {where(video_count > 1)}
Der andere Code für die verschiedenen Modelle ist unter:
class AlbumGroup < ActiveRecord::Base
belongs_to :artist
has_many :collaborations
has_many :artists, through: :collaborations
has_many :albums
has_many :taggings, dependent: :destroy
has_many :artforms, through: :taggings
mount_uploader :artwork, MugshotUploader
def as_json options={}
{
id: id,
title: title
}
end
end
class Album < ActiveRecord::Base
belongs_to :album_group
belongs_to :artist
has_many :tracks
end
class Track < ActiveRecord::Base
has_many :playlist_tracks, dependent: :destroy
has_many :playlists, through: :playlist_tracks
belongs_to :audio
belongs_to :video
has_many :videos
has_many :audios
has_many :taggings, dependent: :destroy
has_many :artforms, through: :taggings
belongs_to :album
belongs_to :artist
default_scope order: position ASC
after_save :cache_columns
def cache_columns
if image_url.nil?
img = album.album_group.artwork_url(:tiny)
unless img.nil?
update_column(:image_url,img)
end
end
if artist_name.nil?
if artist_id
name = Artist.find(artist_id).name
update_column(:artist_name,name)
end
end
if album_name.nil?
if album_id
title = Album.find(album_id).title
update_column(:album_name,title)
end
end
end
end
class Video < ActiveRecord::Base
belongs_to :artist
belongs_to :event
belongs_to :track
has_many :tracks
has_many :playlists, through: :tracks, order: tracks.position ASC
scope :valid, -> {where(flag_count < 2).order(score DESC) }
scope :flagged, -> {where(flag_count > ?, 1) }
# validates :url, uniqueness: {scope: :artist_id}
end