1

I tried to simulate variable_set and variable_get in drupal which use as site-wide variables storage. I tried something like this.

# == Schema Information
# Schema version: 20091212170012
#
# Table name: variables
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  value      :text
#  created_at :datetime
#  updated_at :datetime
#

class Variable < ActiveRecord::Base
  serialize :value

  validates_uniqueness_of :name
  validates_presence_of :name, :value


  def self.set(name, value)
    v = Variable.new() 
    v.name = name
    v.value = value
    v.save
  end  

  def self.get(name)
    Variable.find_by_name(name).value
  end

end

but it doesn't work.

2 Answers 2

1

I've found a way to do this using yaml for storing your values as encoded strings.

Since I'm not storing the "values" on the database, but their converstions to strings, I named the column encoded_value instead of value.

value will be a "decoder-getter" method, transforming the yaml values to their correct types.

class Variable < ActiveRecord::Base
  validates_uniqueness_of :name

  validates_presence_of :name, :encoded_value #change in name

  def self.set(name, value)
    v = Variable.find_or_create_by_name(name) #this allows updates. name is set.
    v.encoded_value = value.to_yaml #transform into yaml
    v.save
  end  

  def self.get(name)
    Variable.find_by_name(name).value
  end

  def value() #new method
    return YAML.parse(self.encoded_value).transform
  end

end

This should return integers, dates, datetimes, etc correctly (not only raw strings). In addition, it should support Arrays and Hashes, as well as any other instances that correctly define to_yaml.

Sign up to request clarification or add additional context in comments.

6 Comments

This is exactly what I looking for the whole week. million thanks :)
I'm glad I could help. By the way, I realized that the name was already set by the find_or_create call - you can remove it if you want.
Where do you know these YAML.parse().transform I can't see it in rails doc.
Problem found on array and hash. It said instance of IO needed
It is because I declared serialize :value, but don't know why this cause a problem.
|
0

I have the following in one of my applications :

class Configure < ActiveRecord::Base
    def self.get(name)
        value = self.find_by_key name
        return value.value unless value.nil?
        return ''
    end

    def self.set(name, value)
        elem= self.find_by_key name

        if elem.nil?
            #We add a new element
            elem = Configure.new
            elem.key = name
            elem.value = value
            elem.save!
        else
            #We update the element
            elem.update_attribute(:value, value)
        end
        return elem.value
    end
end

Which is apparently what you're looking for.

3 Comments

Yes this what is I tried to do, but the problem is the value returned is always a string. Do I need to parse it myself ?
Well yeah. You store the data as a string. It'll always be returned as a string.
I see that. I tried using Marshal.dump and Marshal.load for reserve its data type, but it failed when saving in sqlite for some reason "unrecognized token". I think because after marshal the format is like /004/blablabla and these /number are converted to unknown symbol that not recognized in sqlite. Do you have solutions for this ? or a parameter to save it as /number not alien symbol. Thanks,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.