简体   繁体   中英

Rails 4 model associations conditions

I've got a model that has polymorphic association with conditions.

This however doesn't seem to be working and it sounds like conditions are deprecated in Rails 4.

Below is my code for the association:

has_one :top_background, :class_name => 'UploadedFile', as: :imageable,
            :conditions => { :imageable_type => 'venue_top_background' }, dependent: :destroy

Is there a way to make this work in Rails 4?

In Rails 4, conditions are achieved via a scope block as the second argument to has_one. For example:

has_one :top_background, ->{where(imageable_type: 'venue_top_background')}, :class_name => 'UploadedFile', as: :imageable, dependent: :destroy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM