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

  • SEARCH
  • Home
  • 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 3780682
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:50:51+00:00 2026-05-19T10:50:51+00:00

I have this model: class Event < Registration serialize :fields, Hash Activities=[‘Annonce’, ‘Butiksaktivitet’, ‘Salgskonkurrence’]

  • 0

I have this model:

class Event < Registration
  serialize :fields, Hash
  Activities=['Annonce', 'Butiksaktivitet', 'Salgskonkurrence']

  CUSTOM_FIELDS=[:activity, :description, :date_from, :date_to, :budget_pieces, :budget_amount, :actual_pieces, :actual_amount]
  attr_accessor *CUSTOM_FIELDS

  before_save :gather_fields
  after_find :distribute_fields

  private

  def gather_fields
    self.fields={}
    CUSTOM_FIELDS.each do |cf|
      self.fields[cf]=eval("self.#{cf.to_s}")
    end
  end

  def distribute_fields
    unless self.fields.nil?
      self.fields.each do |k,v|
        eval("self.#{k.to_s}=v") 
      end
    end
  end
end

I have a feeling that this can be done shorter and more elegant. Does anyone have an idea?

  • Jacob

BTW. Can anyone tell me what the asterisk in front of CUSTOM_FIELDS does? I know what it does in a method definition (def foo(*args)) but not here…

  • 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-19T10:50:51+00:00Added an answer on May 19, 2026 at 10:50 am

    Alright first off: never 10000000000.times { puts "ever" } use eval when you don’t know what you’re doing. It is the nuclear bomb of the Ruby world in the way that it can wreak devestation across a wide area, causing similar symptoms to radiation poisoning throughout your code. Just don’t.

    With that in mind:

    class Event < Registration
      serialize :fields, Hash
      Activities = ['Annonce', 'Butiksaktivitet', 'Salgskonkurrence']
    
      CUSTOM_FIELDS = [:activity, 
                     :description,
                     :date_from,
                     :date_to,
                     :budget_pieces,
                     :budget_amount,
                     :actual_pieces,
                     :actual_amount] #1
      attr_accessor *CUSTOM_FIELDS #2
    
      before_save :gather_fields
      after_find :distribute_fields
    
      private
    
      def gather_fields
        CUSTOM_FIELDS.each do |cf|
          self.fields[cf] = send(cf) #3
        end
      end
    
      def distribute_fields
        unless self.fields.empty?
          self.fields.each do |k,v|
            send("#{k.to_s}=", v) #3
          end
        end
      end
    end
    

    Now for some notes:

    1. By putting each custom field on its own line, you increase code readability. I don’t want to have to scroll to the end of the line to read all the possible custom fields or to add my own.
    2. The *CUSTOM_FIELDS passed into attr_accessor uses what is referred to as the “splat operator”. By calling it in this way, the elements of the CUSTOM_FIELDS array will be passed as individual arguments to the attr_accessor method rather than as one (the array itself)
    3. Finally, we use the send method to call methods we don’t know the names of during programming, rather than the evil eval.

    Other than that, I cannot find anything else to refactor about this code.

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

Sidebar

Related Questions

I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
I have a model which describes an event like this: class Event(models.Model): date =
I have this model: class Event_Category(models.Model): event=models.ForeignKey(Event,related_name='event_category') category=models.ForeignKey(Category,related_name='events') user=models.ForeignKey(User) in the view: magazie_cats=Event_Category.objects.filter(event=instance).order_by('category').distinct() return
I have this Django model: from django.contrib.gis.db import models class Event(models.Model): address = models.TextField()
Assume I have this model : class Task(models.Model): title = models.CharField() Now I would
I have this Project model: class Project < ActiveRecord::Base validates :status, :inclusion => {
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
I have a class Similar to this public class Model { public TimeSpan Time1
I have this setup in my models: class Author(models.Model): name = models.CharField(max_length=100) class Topic(models.Model):

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.