Instead, I would use the enum column. Enumerations give you a ton of features like areas, polls, and even hacking methods to change status. But most are listed for expansion β let's say you want to add functionality in which users can have messages that are only viewable by friends β adding an extra state to the listing is easy!
First we add the database column. Run:
rails g migration AddVisiblityToActivities visibility:integer:index
Then edit the transition to add the default value:
class AddVisibilityToActivities < ActiveRecord::Migration def change t.integer :visibility, index: true, default: 0 end end
Start the migration with rake db:migrate . Then we need to add the enumeration mappings to the Activity model:
class Activity < ActiveRecord::Base belongs_to :user has_many :comments, as: :commentable belongs_to :trackable, polymorphic: true
Please note that we are also adding a default scope. With this, we really simplify the request in our controller:
class ActivitiesController < ApplicationController def index
The easiest way to allow users to change visibility when creating / updating records is to use select:
<div class="field"> <%= f.label :visibility %> <%= f.select :visibility, Activity.visibilities.keys.map(&:titleize) %> </div>
Just remember the visibility whitelist!
max
source share