Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7182085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:41:43+00:00 2026-05-28T17:41:43+00:00

I need to store an encoding-state value on a video while it’s been encoded

  • 0

I need to store an encoding-state value on a video while it’s been encoded

I have a video object. While the video is being encoded it needs to lock edits on its comments.

The video therefore needs to store its current encoding state (is it happening yes/no?) and allow child comments to query that property.

Please note

I know that there are better ways to solve this particular problem. I actually need to solve a slightly different problem however I felt the nuances of it would confuse the question so I’ve chosen this one instead. My question is specifically around the nuances of isntance variables and not how to better-solve this encoding problem (which obviously needs a queue).

class Video

  has_many :comments

  after_initialize do
    @encoding_in_process = false
  end

  def encode
    @encoding_in_process = true
    ...
    @encoding_in_process = false
  end

  def encoding_in_process? 
    @encoding_in_process
  end
end

class Comment
  belongs_to :video

  before_update
    raise "locked" if video.encoding_in_process?
  end

  ...
end

As you can see, each video instance is storing an instance variable @encoding_in_process which is used to determine whether a comment can be updated.

The problem

There is a danger there will be multiple in-memory instances of the same video each with different values for @encoding_in_process.

e.g.

bieber_video = Video.find_all_by_artist('Bieber').last
bieber_video.encode 
# assume this takes a while...
bieber_video.encoding_in_process?
# => true

bieber_copy = Video.find_by_id bieber_video.id
bieber_copy.encoding_in_process?
# => false


# Each ActiveRecord objects refer to the same Bieber video
bieber_copy.id == bieber_video.id
# => true

# ...however they refer to different objects in memory:
puts bieber_video
#<Video:0x00000105a9e948>
puts bieber_copy
#<Video:0x00000105a11111>

# and hence each instance has a different version of commenting_locked?
# bieber_video.encoding_in_process? != bieber_copy.encoding_in_process?

The question

Given that the same database row might generate two different in-memory instances, what is a safe way to store transient non-database-backed information about those instances?

EDIT

The actual problem I’m trying to solve is setting a flag on an object when destroy is initiated such that its child objects can determine whether or not they’re eligible to be destroyed themselves.

It’s therefore a very instantaneous problem and not suitable for backing into the database. I used this video example because I thought it was a bit clearer however I may have simply muddied the waters.

THE SOLUTION (courtesy of one of the answers below

@Alex D’s suggestion did solve the problem but to add further clarity to this for anyone wanting to repeat, the actual code was this:

class Video
  # set a class variable containing an array of all videos
  # which are currently being encoded
  @@ids_of_videos_being_encoded = []
  ...

  def encode
    store_encoding_state true
    begin
      encode()
    ensure
      # make sure we switch this off after 
      # encoding finishes or fails
      store_encoding_state false
    end
  end

  private  
    def store_encoding_state encoding_in_progress
      if encoding_in_progress
        @@ids_of_videos_being_encoded.push(id)
      else
        @@ids_of_videos_being_encoded.delete(id)
      end
    end

    def encoding_initiated?
      @@ids_of_videos_being_encoded.include? id
    end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:41:43+00:00Added an answer on May 28, 2026 at 5:41 pm

    The answer to your question depends on whether you may use multiple server processes or not. If you may want to run multiple server processes (which is a good assumption), the problem is not just multiple in-memory ActiveRecord objects representing the same DB row, the problem is multiple objects in different memory spaces.

    If you have multiple processes which are somehow collaboratively working with the same data, you must keep that data in a shared store (i.e. a database), and you must flush changes to the store, and refresh your in-memory data as needed. In this case, you cannot rely on transient in-memory data being kept in synchronization (because there is no way it possibly could be).

    If constantly writing/reading your transient data to the DB sounds expensive, that’s because it is. In general, whenever you have multiple processes (on the same or different servers) working together, you want to design things so each process can grab a chunk of data and work on it for a while without having to communicate with the others. Fine-grained data sharing in a distributed system = bad performance.

    If you are sure that you will only ever use a single server process, and you want to simulate the effect of instance variables which are shared between multiple ActiveRecord objects representing the same DB row, keep the data in a hash, keyed by the record ID, and use getters/setters which read/write the hash. If you are doing a lot of this, you can do some metaprogramming “magic” to have the getters/setters automatically generated (a la “attr_accessor”). If you need help writing that metaprogramming code, post a question and I’ll answer it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have id values for products that I need store. Right now they are
I need to store a list of key value pairs of (integer, boolean) in
I need to store certain information while my application is executing and again fetch
I have a site that needs to encrypt and store binary files that are
If you have binary data that you need to encode, what encoding scheme do
I have a string coming in, which I need to store in the db.
I need to store products for an e-commerce solution in a database. Each product
I need to store phone numbers in a table. Please suggest which datatype should
I need to store app specific configuration in rails. But it has to be:
I need to store user entered changes to a particular table, but not show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.