Lesson 1 — Setup and seeding
Update the Membership model
The memberships table already has a role integer column but the
model doesn’t define the enum. Add it:
|
|
This gives us membership.admin?, membership.member?,
Membership.admins, Membership.members, and the role: symbol
interface throughout.
Update seeds
The existing seeds file has two problems: users have no password so
they can’t sign in, and Alice gets a duplicate admin membership because
Board.create! already triggers add_owner_as_member.
Replace db/seeds.rb:
|
|
Reset and reseed:
|
|
db:seed:replant truncates all tables and re-seeds without
re-running migrations. You now have three users — sign in as any of
them with the password password.
Verify membership
|
|
Update BoardsController to use membership scope
Currently BoardsController#index shows owned_boards. Update it to
show all boards the user is a member of — which now includes shared
boards:
|
|
current_user.boards uses has_many :boards, through: :memberships —
it returns all boards where a membership record exists, including boards
the user owns (since add_owner_as_member creates that record).