简体   繁体   中英

Make Rails use schema.rb instead of reading the schema from the database

Why ActiveRecord queries the database to know the database schema? Can't it simply read the db/schema.rb ?

I have some Sidekiq workers that for performance cannot connect to the database. The job itself doesn't use the database at all (or at least I expected so):

n = Notification.new
n.body = cache["body"] # cache is from Redis
...

But the first line actually calls the database!

Is there anything I can do to make Rails read the schema.rb , or, in any case, instantiate a Notification without the database?

I don't want to create a separate model that doesn't inherits from ActiveRecord. I need the same model: sometimes it's loaded from the database and sometimes from Redis.

You can create a separate object which represents the Redis version of the object and use an included module to share methods between the AR and Redis versions. ActiveRecord::Base instances are not designed to be used without the database.

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