record count by month in rails

# Step 1: Add a date column to your model (assuming the model is named 'YourModel').
# Run the following migration:
rails generate migration AddDateToYourModel date:date

# Step 2: Run the migration to apply changes to the database:
rails db:migrate

# Step 3: In your model (app/models/your_model.rb), add a scope to extract month and year from the date column:
scope :by_month, ->(date) { where('extract(month from date) = ? AND extract(year from date) = ?', date.month, date.year) }

# Step 4: In your controller or wherever you need the record count by month, use the following code:
@monthly_counts = YourModel.group("DATE_TRUNC('month', date)").count

# Step 5: In your view, you can display the record count by month:
<% @monthly_counts.each do |month, count| %>
  <p><%= "Month: #{month.strftime('%B %Y')}, Count: #{count}" %></p>
<% end %>