Losing an Attribute When Saving Through an Association w/ Scope (Rails 4.0.0)

前端 未结 1 1408
执念已碎
执念已碎 2021-01-12 12:38

The Code (Rails 4.0.0)

class Track < ActiveRecord::Base
  has_many :artist_tracks
  has_many :owning_artists,
              -> { where(:artist_tracks         


        
相关标签:
1条回答
  • 2021-01-12 13:05

    I believe that what is happening is that the associated model actually being created here is the join model, artist_tracks, and not the association with the actual conditions on it. You could probably fix this by declaring an alternate join association with conditions on it, and then attaching owning_artists through that instead. Like this:

    class Track < ActiveRecord::Base
      has_many :artist_tracks
      has_many :owning_artist_tracks,
                  -> { where(:artistic_role_id => 1) },
                  :class_name => "ArtistTrack"
      has_many :owning_artists,
                  :through => :owning_artist_tracks,
                  :source => :artist
    end
    
    0 讨论(0)
提交回复
热议问题