I wanted to make that the user can endorse each user once a month and 3 users in total a month.

First of all, got a method in user model:

```
# Returns true if the current user is endorsing the other user.
  def endorsing?(other_user)
    endorsing.include?(other_user)
  end

```

Want to slightly change it to check if the user already endorsed the user this month.
I belive it needs to look something like this:

```
def endorsing?(other_user)
    endorsing.where(:created_at => (Time.zone.now.beginning_of_month..Time.zone.now)).include?(other_user)
end

```

But that is obviously wrong cause it gives me following SQL query:

```
SELECT "users".* FROM "users" INNER JOIN "endorsements" 
ON "users"."id" = "endorsements"."endorsed_id"
WHERE "endorsements"."endorser_id" = ? AND ("users"."created_at" BETWEEN '2016-01-01 00:00:00.000000' AND '2016-01-26 17:15:53.700307')  [["endorser_id", 1]]

```

the `"users"."created_at"` should be `"endorsements"."created_at"`

How do I do that?

Same counts for the limit problem I've got:

```
def endorsement_count_within_limit?
    if endorser.endorsing.where(:created_at => (Time.zone.now.beginning_of_month..Time.zone.now)).count >= 3
      errors.add(:base, "Exceeded endorse limit (3) this month")
    end
  end

```

Which gives:

```
SELECT COUNT(*) FROM "users" INNER JOIN "endorsements"
ON "users"."id" = "endorsements"."endorsed_id"
WHERE "endorsements"."endorser_id" = ? AND ("users"."created_at" BETWEEN '2016-01-01 00:00:00.000000' AND '2016-01-26 17:15:53.708638' [["endorser_id", 1]]

```

Same problem, users.created should be endorsements.created

I have no idea how to fix that, And bright ideas?

Below i'll paste my controllers, models and partials that use the methods to render forms for endorsing:

---

Endorsements model:

```
class Endorsement < ActiveRecord::Base
  belongs_to :endorser, class_name: "User"
  belongs_to :endorsed, class_name: "User"
  validates :endorser_id, presence: true
  validates :endorsed_id, presence: true
  validates :comment, presence: true, length: { maximum: 140}
  validate :endorsement_count_within_limit?, :on => :create

  def endorsement_count_within_limit?
    if endorser.endorsing.where(:created_at => (Time.zone.now.beginning_of_month..Time.zone.now)).count >= 3
      errors.add(:base, "Exceeded endorse limit (3) this month")
    end
  end

end

```

Endorsements controller:

```
class EndorsementsController < ApplicationController
  before_action :logged_in_user

  def new
  end

  def create
    @user = User.find(params[:endorsed_id])
    comment = params[:endorsement][:comment]
    current_user.endorse(@user, comment)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end

  def destroy
    @user = Endorsement.find(params[:id]).endorsed
    current_user.unendorse(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end

end

```

User model:

```
class User < ActiveRecord::Base
  has_many :microposts, dependent: :destroy
  has_many :active_endorsements, class_name: "Endorsement",
                                 foreign_key: "endorser_id",
                                 dependent: :destroy
  has_many :passive_endorsements, class_name:  "Endorsement",
                                  foreign_key: "endorsed_id",
                                  dependent:   :destroy
  has_many :endorsing, through: :active_endorsements, source: :endorsed
  has_many :endorsers, through: :passive_endorsements, source: :endorser

.
.
.
  # Endorses a user.
  def endorse(other_user, comment)
    active_endorsements.create(endorsed_id: other_user.id, comment: comment)
  end

  # Unendorses a user.
  def unendorse(other_user)
    active_endorsements.find_by(endorsed_id: other_user.id).destroy
  end

  # Returns true if the current user is endorsing the other user.
  def endorsing?(other_user)
    endorsing.where(:created_at => (Time.zone.now.beginning_of_month..Time.zone.now)).include?(other_user)
  end

  private
.
.
.

end

```

Partials:

\_endorse\_form:

```
<% unless current_user?(@user) %>
  <div id="endorse_form_<%= @user.id %>">
  <% if current_user.endorsing?(@user) %>
    <%= render partial: 'shared/unendorse' %>
  <% else %>
    <%= render partial: 'shared/endorse'%>
  <% end %>
  </div>
<% end %>

```

\_endorse:

```
<%= form_for(current_user.active_endorsements.build, remote: true) do |f| %>
  <% if f.object.endorsement_count_within_limit? %>
    <div id="error_explanation">
      <div class="alert alert-danger">
        The form contains <%= pluralize(f.object.errors.count, "error") %>.
      </div>
      <ul>
      <% f.object.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% else %>
    <div><%= hidden_field_tag :endorsed_id, @user.id %></div>
    <%= f.submit "Endorse", class: "btn btn-primary" %>
    <%= f.text_field :comment, class: 'form-control' %>
  <% end %>
<% end %>

```

\_unendorse:

```
<%= form_for(current_user.active_endorsements.find_by(endorsed_id: @user.id),
             html: { method: :delete },
             remote: true) do |f| %>
  <%= f.submit "Remove endorse", class: "btn" %>
<% end %>

```

If you need to look at other files it's available here with some parts missing:<https://bitbucket.org/kramarz/pracainzynierska>