简体   繁体   中英

Unscoped work in rails console but not work in controller action

I have model with default_scope:

class Campaign < ActiveRecord::Base
  default_scope where("campaigns.status != ?", "archive")
end

Then I need disable this scope in show action:

@campaign = Campaign.unscoped.find(params[:id])

But I see Couldn't find Campaign with id=1 [WHERE (campaigns.status != 'archive')]

Then I tried the same solution in rails console:

 Campaign.unscoped.find(1)
 SELECT `campaigns`.* FROM `campaigns` WHERE `campaigns`.`id` = 1 LIMIT 1 

And all works fine, what I doing wrong in controller?

UPDATE:

Campaign.unscoped{ }

Did not work too. It seems @campaign determined elsewhere and earlier, because I delete all rows from my show action and now it looks like:

def show
end

And I am still getting this error: Couldn't find Campaign with id=1 [WHERE (campaigns.status != 'archive')]

I find out this tricks in load_and_authorize_resource , but how can I fix it?

Ref this

Campaign.unscoped {
  Campaign.find(params[:id]) 
}

It seems like what you have would work fine, according to the documentation. However, it is mentioned here that it is recommended to use the block form of unscoped. You might give that a try and see if it resolves the problem.

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