rubin pe șine de filtrare pg_search_scope v2.2

voturi
46

Sunt foarte nou la Ruby on Rails si am nevoie de ajutor imaginind cum să modifice o interogare db existentă. Sunt folosind versiunile vechi, care nu pot actualiza: Ruby 2.2.3p173, și Rails 4.0.2.

Vreau să filtreze rezultatele interogării existente pentru a elimina înregistrările care nu au niciun videoclip. Ierarhia modelului cred că este: Artist, AlbumGroup, album, Track, video.

Pentru a clarifica: Vreau artiști cu cel puțin un video, de la asociația modelul interpret-> AlbumGroup-> albums->> tracks- video (nu interpret-> videoclipuri).

Query-ul este conținută în modelul Artist:

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

Vreau să adaug ceva de genul următoarele pentru a filtra înregistrările care nu au niciun videoclip: (la interogare):

if: artist.releases.albums.tracks.videos.count > 1

Sau modelul artist poate ?:

scope :valid, -> {where(video_count > 1)}

Celălalt cod pentru diferite modele este de mai jos:

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
Întrebat 07/11/2017 la 14:20
de către utilizator
În alte limbi...                            


2 răspunsuri

voturi
0

ActiveRecord lui has_manyși belongs_tometode pe care le dau posibilitatea de a defini lunete direct pe asociație. În acest fel, puteți personaliza SQL care este generat atunci când accesați asociația.

Să presupunem că a dorit să definească un domeniu de aplicare pe modelul Artist care returnează toți artiștii care au videoclipuri la zero. În Rails 5, pentru a găsi toți artiștii care nu au videoclipuri, puteți utilizaleft_outer_joins

class Artist < ActiveRecord::Base
  has_many :videos
  scope :with_zero_videos, -> { left_outer_joins(:videos).where(videos: {id: nil}) }
end

O altă soluție bună este de a folosi includesca aceasta:

Artist.includes(:videos).where(videos: { videos_id: nil })

Puteți alege unul care vă place cel mai mult!

Sper că a ajutat.

Publicat 07/11/2017 la 15:12
sursa de către utilizator

voturi
0

Multumesc Gabriel, am adăugat o interogare SQL brut la artists_controller.rb, care arata ca:

  def index
    #artists = Artist.for_name(params[:name])
    artists = Artist.find_by_sql ["SELECT artists.*, ((artists.popularity / 50 * (ts_rank((artists.tsv_name), (to_tsquery('simple', ''' ' || ? || ' ''' || ':*')), 2))) + (ts_rank((artists.tsv_name), (to_tsquery('simple', ''' ' || ? || ' ''' || ':*')), 2))) AS pg_search_rank FROM artists " +
      "JOIN album_groups on (artists.id = album_groups.artist_id) " +
      "JOIN tracks on (tracks.album_group_id = album_groups.id) " +
      "JOIN videos on (videos.track_id = tracks.id) " +
      "WHERE (((artists.tsv_name) @@ (to_tsquery('simple', ''' ' || ? || ' ''' || ':*')))) " +
      "GROUP BY artists.id " +
      "ORDER BY pg_search_rank DESC, artists.id ASC", params[:name], params[:name], params[:name]]
    render json: artists, each_serializer: SimpleArtistSerializer
  end
Publicat 08/11/2017 la 13:42
sursa de către utilizator

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more