简体   繁体   中英

Rails 4 scope model/controller

In my model I've defined:

scope :active, -> { where(active: 'true') }

And my controller

def index
    if logged_in? 
     @objects = Objects.all
    else 
    @objects = Objects.active
    end 
  end

In my index view, when logged_in? is true, I get all records as expected, but otherwise, I get nothing (I know at least one record is active).

Have I written my scope incorrectly?

It should be

scope :active, -> { where(active: true) }

Note: true is not enclosed in quotes

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