Reputation: 341
I am completely stuck on how to do this and hopefully, someone can point me in the correct direction. I have a rails app that has crewmembers, which are part of departments and each has the positions. I am looking to make a master list feature where a page is created with all the crewmembers' names and their respective position, grouped by department. In the creation phase I would like the user to be able to remove names from the list that they don't want to print. However, I don't want the actual crewmembers to be deleted. Any insight would be great.
Upvotes: 2
Views: 46
Reputation: 36
You can add a field named :visible
, by running rails generate migration add_visible_to_crewmembers visible:boolean
.
Add a default value (of true
, as you want the crewmembers to be normally visible) to your migration:
def up
change_column :visible, :boolean, default: true
end
def down
change_column :visible, :boolean, default: nil
end
Persist these changes to your database, by running rake db:migrate
.
After that, you can create a scope in your Crewmember model (apps/models/crewmember.rb
) to return only the visible crewmembers, like this:
class Crewmember < ActiveRecord::Base
belongs_to :department
[...]
scope :visible, where(visible: true)
end
In your controller, use this scope instead of the default one (or using #all
, for instance):
def index
# Before:
# @crewmembers = Crewmember.all
# @crewmembers.size => 10
@crewmembers = Crewmember.visible
# @crewmembers.size => 5
end
Upvotes: 2