I tried to make a group function (bulletin board) with Rails

Creating a group function (bulletin board style) with Rails


Development environment

  1. ruby 2.6.3
  2. Rails 5.1.6
  3. AWS Cloud9
  4. GitHub
  5. sqlite3 (develop environment)

Design concept

Users are free to create groups and can also belong to groups created by other users. Members who join the group will also be able to speak freely within the group.

If you put this mechanism on the table, Since users and groups have a many-to-many relationship, we will use an intermediate table. And since there is a many-to-many relationship between users and posts within the group, we also use an intermediate table. IMG_8162 (1).jpg

The ER diagram looks like this. (I'm sorry for handwriting ...)

model

The previous ER diagram has the following associations.

group.rb


class Group < ApplicationRecord
  validates :name, presence: true, uniqueness: true
  
  has_many :group_users
  has_many :users, through: :group_users
  has_many :groupposts
  accepts_nested_attributes_for :group_users
end

group_user.rb


class GroupUser < ApplicationRecord
  
  belongs_to :group
  belongs_to :user
end

grouppost.rb


class Grouppost < ApplicationRecord
  belongs_to :group
  belongs_to :user
end

controller

groups_controller.rb


class GroupsController < ApplicationController
  def new
    @group = Group.new
    @group.users << current_user
  end
  
  def create
    if Group.create(group_params)
      redirect_to groups_path, notice: 'I created a group'
    else
      render :new
    end
  end
  
  def index
    @groups = Group.all.order(updated_at: :desc)
  end
  
  def show
    @group = Group.find_by(id: params[:id])
    
    if [email protected]?(current_user)
      @group.users << current_user
    end
    
    @groupposts = Grouppost.where(group_id: @group.id).all
  end
  
  private
  def group_params
    params.require(:group).permit(:name, :user_id [])
  end
  
  def grouppost_params
    params.require(:grouppost).permit(:content)
  end
  
end

The basic design is the same as around the user.

def show
.
.

  if [email protected]?(current_user)
   @group.users << current_user
  end
end

In this way, people who follow the link of a group can belong to that group.

grouppost_controller.rb


class GrouppostsController < ApplicationController
  
  def new
    @grouppost = current_user.groupposts.new
    @group = Group.find_by(id: params[:group_id])
  end
  
  def create
    @group = Group.find_by(id: params[:group_id])
    @grouppost = current_user.groupposts.new(grouppost_params)
    @grouppost.group_id = params[:group_id]
    if @grouppost.save
      redirect_to group_path(@group.id)
    end
  end
  
  private
    def grouppost_params
      params.require(:grouppost).permit(:content)
    end
end

This also has the same shape as the posting function created earlier.

With the above, the 2ch small group function (bulletin board style) is completed. As a future task, I would like to be able to lock when creating a group and create an invitation-only group.

Recommended Posts

I tried to make a group function (bulletin board) with Rails
I tried to make a login function in Java
I want to make a function with kotlin and java!
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried to make a message function of Rails Tutorial extension (Part 1): Create a model
Make a login function with Rails anyway
I want to add a browsing function with ruby on rails
I tried to implement the image preview function with Rails / jQuery
I tried to make a reply function of Rails Tutorial extension (Part 3): Corrected a misunderstanding of specifications
I tried to make a message function of Rails Tutorial extension (Part 2): Create a screen to display
I tried to make Basic authentication with Java
How to make a follow function in Rails
I tried to break a block with java (1)
[iOS] I tried to make a processing application like Instagram with Swift
I tried to make a Web API that connects to DB with Quarkus
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
I tried to implement a function equivalent to Felica Lite with HCE-F of Android
Introduced # 10 devise_token_auth to build a bulletin board API with authentication authorization in Rails 6
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
Introducing # 15 pundit to build a bulletin board API with authentication authorization in Rails 6
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
I want to define a function in Rails Console
I tried to create a shopping site administrator function / screen with Java and Spring
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished version ②)
I tried to make an introduction to PHP + MySQL with Docker
I tried to modernize a Java EE application with OpenShift.
I want to make a list with kotlin and java!
[Rails] I tried to implement batch processing with Rake task
I tried to make a client of RESAS-API in Java
[Rails] I tried to implement "Like function" using rails and js
I tried to create a padrino development environment with Docker
I tried to interact with Java
I tried to implement Ajax processing of like function in Rails
I want to make a button with a line break with link_to [Note]
[Unity] I tried to make a native plug-in UniNWPathMonitor using NWPathMonitor
I tried to build a simple application using Dockder + Rails Scaffold
I tried to make an Android application with MVC now (Java)
[Java] I tried to make a maze by the digging method ♪
Rails Tutorial Extension: I tried to create an RSS feed function
I tried to get started with WebAssembly
[Rails withdrawal] Create a simple withdrawal function with rails
I tried to make a machine learning application with Dash (+ Docker) part2 ~ Basic way of writing Dash ~
I tried playing with BottomNavigationView a little ①
Make a site template easily with Rails
I tried to implement ModanShogi with Kinx
I made a bulletin board using Docker 1
# 16 policy setting to build bulletin board API with authentication authorization in Rails 6
# 8 seed implementation to build bulletin board API with authentication authorization in Rails 6
I tried to make a parent class of a value object in Ruby
I tried to make a simple face recognition Android application using OpenCV
[Rails] I tried to implement a transaction that combines multiple DB processes
I tried to make an automatic backup with pleasanter + PostgreSQL + SSL + docker
I tried to build a Firebase application development environment with Docker in 2020
I made a virtual currency arbitrage bot and tried to make money
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
Build a bulletin board API with authentication authorization in Rails # 13 Add authentication header
Introduced # 9 serializer to build bulletin board API with authentication authorization in Rails 6
I tried to make a talk application in Java using AI "A3RT"
I tried to make a machine learning application with Dash (+ Docker) part1 ~ Environment construction and operation check ~
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
Create a simple bulletin board with Java + MySQL