9

I have an organization table for example

ID, name, address, phone, email, etc...

Now I want to add multiple addresses, phones and emails.

Is it a good way to store json data in email column like this

[ "[email protected]", "[email protected]", "[email protected]" ]

Or create another table just for emails and another for phones etc...

If storing json data is better - what is the best way to use it in rails?

3 Answers 3

8

Here is what I found

http://api.rubyonrails.org/classes/ActiveRecord/Base.html

Saving arrays, hashes, and other non-mappable objects in text columns

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

Comments

1

Storing data as a JSON string in a single database field means that you will not be able to manipulate/query the data using SQL - which kind of defeats the point of storing the data in a database, you may as well store it in a text file.

I'd recommend a one-to-many relationship between your organization table and tables for email addresses and phone numbers. See video explaining different relationship types

3 Comments

Thansk. I think that sql way is better but here is the problem. I am not using multiple emails only in "organization" table. For example if I have 4 tables and each of them needs multiple phones, emails and addresses is this a best way to create "organization_emails", "organization_phones", "organization_addresses", "othertable_emails", etc...?
In this case you need a 'junction' or 'join' table (en.wikipedia.org/wiki/Junction_table) so your join table might contain the following fields: ID (this will be the othertable.id or organizations.id), Type (this will identify which table this record is for, I usually just have this as varchar and use a string to identify and index it) and telephone_id this will tell you where to fetch the telephone number from
Or if you have only got 2 tables you could just have organization_id and othertable_id in your telephone table
0

suggest you store those information in a single table. according to your requirement.it seems use a good polymorphic model will be better.

The code may like this.

module MultiAttr

  def self.included(base)
    base.send :extend, ClassMethods
  end

  module ClassMethods
    def multi_attr(*args)
      args.each do |attr_name|
        class_eval <<-EOF
          has_many attr_#{attr_name}, :class_name => "MultiAttributes", :as => :owner,
             :conditions => {:key => '#{attr_name.singularize}'}

          def add_#{attr_name.singularize}(val)
            self.attr_#{attr_name}.create(:key => #{attr_name.singularize}, :value => val)
            #{attr_name}
          end

          def #{attr_name}
            self.attr_#{attr_name}.map(&:to_attr_model)
          end

        EOF
      end
    end

  end


end

class AttrModel < String

  def initialize(record)
    @record = record
    super(record.value)
  end

  def remove
    @record.destroy
  end

end


#owner_type, owner_id, key, value
class MultiAttribute < ActiveRecord::Base
  belongs_to :owner, :polymorphic => true

  def to_attr_model
    @attr_model ||= AttrModel.new(self)
  end
end

how to use

class User < ActiveRecord::Base
  include MultiAttr
  multi_attr :emails, :addresses
end

user.emails #=> ["[email protected]"]
user.add_email "[email protected]" #=> 
user.emails.first.remove

these codes not tested. but its my basic idea.

Comments

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.