rubin på skinner filtrering pg_search_scope v2.2

stemmer
46

Jeg er veldig nytt for Ruby on Rails og jeg trenger hjelp til å finne ut hvordan du kan endre en eksisterende db spørring. Jeg bruker gamle versjoner, som jeg ikke kan oppdatere: Ruby 2.2.3p173, og Rails 4.0.2.

Jeg ønsker å filtrere eksisterende spørringsresultatene for å fjerne poster som ikke har noen videoer. Modellhierarkiet jeg tror er: Artist, AlbumGroup, album, spor, Video.

For å klargjøre: Jeg ønsker artister som har minst ett video, fra modellen foreningen artist> AlbumGroup-> albums-> spor-> videoer (ikke artist> videoer).

Den eksisterende spørring finnes i modell 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

Jeg vil legge til noe sånt som følgende for å filtrere ut de postene som ikke har noen videoer: (i søket):

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

Eller Artist modellen kanskje ?:

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

Den andre koden for de ulike modellene er under:

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
Publisert på 07/11/2017 klokken 14:20
bruker
På andre språk...                            


2 svar

stemmer
0

Active s has_manyog belongs_tometoder gi deg muligheten til å definere omfang direkte på foreningen. På denne måten kan du tilpasse SQL som genereres når du åpner foreningen.

la oss si at du ønsket å definere et omfang på Artist modell som returnerer alle kunstnere som har null videoer. I Rails 5, for å finne alle artister som ikke har noen videoer, kan du brukeleft_outer_joins

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

En annen god løsning er å bruke includessom dette:

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

Du kan velge den du liker best!

Håper det hjalp.

Svarte 07/11/2017 kl. 15:12
kilden bruker

stemmer
0

Takket Gabriel, jeg har lagt til en rå SQL-spørring til artists_controller.rb, som ser slik ut:

  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
Svarte 08/11/2017 kl. 13:42
kilden bruker

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