0

I have a collection of checkboxes of names that a user can select multiple of.

I want to save the users selection for the next time they login.

However, over time some names may get removed while new ones may get added. Added names can remain unchecked, but I wouldn't want to keep storing IDs of deleted names.

So how should I go about this?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
99miles
  • 10,942
  • 18
  • 78
  • 123

1 Answers1

0

I would create a table for your options, then have a join table that records the checked options

Then you can add :dependent => :destroy to the relationship on the options side. That would remove the record in the join table when the option is removed.

Here is an example of a many to many:

def User < ActiveRecord::Base
   has_many :preferences, :dependent=>:destroy
   has_many :options, :through=>:preferences
end

def Preferences < ActiveRecord::Base
    belongs_to :user
    belongs_to :option
end

def Option < ActiveRecord::Base
    has_many :preferences, :dependent=>:destroy
    has_many :users, :through=>:preferences
end

If you need to you can create multiple join tables, for the different kind of options. Say for background color you need a join that allows you to also store the color with the association, or maybe for pagination you need to store an integer for articles on a page, that could be another model. Or you could just add a single string value tot he join table and store associated data as a string and use only the one.

Ben Miller
  • 1,464
  • 12
  • 13
  • That's a great suggestion, however wouldn't that require a new table simply for storing this preference? I was expecting to have a table for storing all preferences but it seems like a new table for various preferences could add up, but maybe that's just the way it goes. – 99miles Apr 12 '12 at 05:31
  • no, You have a single table that has each preference as a record, then a join table from user to preferences. You add a new preference by adding a row to the preferences table, to mark the new preference as active for a user you add a record to the join the table. – Ben Miller Apr 12 '12 at 13:10
  • If my table stores many preferences, yet one (or more) of the preferences is a has_many, how does that work? Let's say I'm storing a collection of names as initially mentioned, as well as the users choice of background_color. How would that table and join look if some preferences are one-to-many? id, account_id, background_color, names_selection (but there can be many of these names selected, not just one) so I'm not sure how your suggested one_to_many approach would work here) – 99miles Apr 15 '12 at 05:01
  • We that is different than a list of check boxes that the user can select multiple of as you stated in the question. – Ben Miller Apr 15 '12 at 12:50
  • What i was describing is not a one to many, it is a many to many. When you have a join table to store the association, you are creating a many to many. See my updated example for more info. – Ben Miller Apr 15 '12 at 15:20