从参与活动的数据库中获取所有用户

时间:2017-06-12 15:39:20

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我目前面临以下问题:用户可以创建活动。其他用户有可能参加活动。我现在想要已经参与活动的用户将列在活动视图中。因此,我尝试使用activity_id显示参与activity_controller的show方法中的活动的所有用户。

模型的关系如下:

class ActivitiesController < ApplicationController
  before_action :set_activity, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:show]

  def index
    @activities = Activity.all
  end

  def show
    @participated = Participation.where("activity_id = ? AND user_id = ?", @activity.id, current_user.id).present? if current_user
    @reviews = @activity.reviews
    @hasReview = @reviews.find_by(user_id: current_user.id) if current_user

    @participant = Participation.where (:activity_id == 'activity_id')
  end



  def new
    @activity = current_user.activities.build
  end

  def create
    @activity = current_user.activities.build(activity_params)

    if @activity.save

      redirect_to edit_activity_path(@activity), notice: "We want to give you a warm welcome!"

    else
      render :new, notice: "Something seems to be missing!"
    end
  end

  def edit
    if current_user.id == @activity.user.id
      redirect_to root_path
    end
  end

  def update
    if @activity.update(activity_params)
      redirect_to edit_activity_path(@activity), notice: "Updated!"
    else
      render :edit
    end
  end

  def destroy
    @activity = Activity.find(params[:id])

    if @activity.destroy
      redirect_to activities_path, notice: "Deleted!"
    end
  end

  private

  def set_activity
    @activity = Activity.find(params[:id])
  end


  def activity_params
    params.require(:activity).permit(:activity_name,
                                     :activity_starttime,
                                     :activity_duration,
                                     :activity_intensity,
                                     :activity_distance,
                                     :activity_topography,
                                     :activity_address,
                                     :activity_track,
                                     :activity_summary)

  end
end

我的活动控制器正在执行以下操作:

    class ParticipationsController < ApplicationController
  before_action :authenticate_user!

  def preload
    activity= Activity.find(params[:activity_id])
    participations = activity.participations
    render json: participations
  end

  def create

    if Participation.exists?(user_id: current_user.id)

      redirect_to activities_path(@activity), notice: "Du nimmst bereits teil!"

    else

      @participation = current_user.participations.create(participation_params)
      redirect_to @participation.activity, notice: "Klasse, du nimmst nun teil!"

    end

  end


  def participant
    @participant = user.participations
  end

  def your_matches
    @matches = current_user.participations
  end

  def your_participations
    @activities = current_user.activities
  end

  private

  def participation_params
    params.permit(:activity_id, :status)
  end
end

同时参与控制器处理这个:

<div class="row">
  <div class="col-md-12">
    <h3>Wer kommt bis jetzt mit? </h3>


    <table>
      <tr>
        <th></th>
      </tr>

      <% @participant.each do |participant| %>

          <tr>
            <td><%= participant.user.fullname %></td>
          </tr>

      <% end %>

    </table>

  </div>
</div>

在我看来,我使用以下方法调用控制器:

<div class="panel-body">
    <% @activities.each do |activity| %>
      <% activity.participations.each do |participation| %>


      <div class="row">

        <div class="col-md-3">
          <%= link_to user_path(participation.user) do %>
              <%= participation.user.fullname %>
          <% end %>
      </div>

这为我提供了参与任何活动的所有用户的列表。但我现在唯一想要的是显示参与所请求活动的所有用户。我会说我的关系应该正常工作,因此我想我只需要一个小提示来解决问题。在我的应用程序的另一点,我已经有一个列表,我可以显示参与的用户,但由于不同的控制器,我无法从活动控制器调用此。总而言之,这会导致错误,因为我无法从activitities_view调用participations_controller。

class CreateParticipations < ActiveRecord::Migration
  def change
    create_table :participations do |t|
      t.references :user, index: true, foreign_key: true
      t.references :activity, index: true, foreign_key: true

      t.string :status

      t.timestamps null: false
    end
  end
end


class CreateActivities < ActiveRecord::Migration
  def change
    create_table :activities do |t|

      t.string :activity_name
      t.datetime :activity_starttime
      t.time :activity_duration
      t.string :activity_intensity
      t.integer :activity_distance
      t.string :activity_topography
      t.string :activity_address
      t.string :activity_track
      t.string :activity_summary


      t.references :user, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

非常感谢任何帮助和提前感谢!

编辑1:有关数据库包含的更多信息:

body {
  font-family: "PT Sans";
  background-color: #dfc;
  color: #000;
}

.top {
  width: 100%;
  height: 50px;
  position: fixed;
  left: 0;
  top: 0;
  background-color: #5dbf61;
  z-index: 100;
}

.stickyTop {
  width: 100%;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 100;
}

.logo {
  display: block;
  font-size: 26px;
  padding: 5px;
  padding-left: 10px;
  text-align: center;
  letter-spacing: 1px;
}

.logo a {
  text-decoration: none;
  color: #fff;
  cursor: pointer;
}

.sidebar {
  width: 250px;
  height: 100%;
  position: fixed;
  left: 0;
  top: 50px;
  background-color: #4caf50;
  z-index: 99;
  overflow-y: auto;
  overflow-x: none;
}

.stickySidebar {
  height: 100%;
  position: fixed;
  left: 0;
  top: 50px;
  z-index: 99;
}

.sidebar>.option {
  height: 42px;
  line-height: 40px;
  width: 100%;
  left: 0;
  display: inline-block;
  text-indent: 12px;
  color: #dfc;
}

.sidebar a {
  display: block;
  text-decoration: none;
  color: #dfc;
}

.sidebar>.option:hover {
  background-color: #8bc34a;
}

.sidebar>.selected {
  height: 42px;
  line-height: 40px;
  width: 100%;
  left: 0;
  display: inline-block;
  background-color: #8bc34a;
  text-indent: 12px;
  color: #dfc;
}

.sidebar>.panel {
  width: 226px;
  /*250 - 12 - 12*/
  left: 0;
  display: inline-block;
  padding: 12px;
  color: #dfc;
  font-size: 14px;
}

.sidebar>.empty {
  height: 42px;
  width: 100%;
  left: 0;
  display: inline-block;
  text-indent: 12px;
  color: #dfc;
}

.sidebar>.empty-border {
  height: 42px;
  width: 100%;
  left: 0;
  display: inline-block;
  text-indent: 12px;
  color: #dfc;
}

.navbar {
  width: 100%;
  height: 26px;
  left: 250px;
  top: 50px;
  position: fixed;
  display: block;
  z-index: 99;
  padding-left: 9px;
  padding-top: 4px;
  border-bottom: 2px solid #9b8;
  background-color: #bda;
  color: #796;
  font-size: 14px;
}

.stickyNavbar {
  height: 100%;
  position: fixed;
  left: 250px;
  top: 50px;
  z-index: 99;
}

.navbar a {
  color: #796;
}

.content {
  width: 100%;
  height: 100%;
  left: 250px;
  top: 76px;
  position: fixed;
  display: block;
  padding: 15px;
}

.spaced-text {
  letter-spacing: 1px;
}

.fixed-center {
  width: 100%;
  display: block;
  text-align: center;
}

1 个答案:

答案 0 :(得分:0)

I still don't understand when you add a has_many and belongs_to relantionship in your Participation model with reference to the Activity model.

If you'd have:

  • User has_many participations
  • Activity has_many participations
  • Participation belongs_to user and belongs_to activity

Then you could join User with Participation in a query and try to get all the users that have participated to a particular activity like:

User.joins(:participations).where('participations.activity_id = 1')

Edit: I've tested your code, without touching nothing in the models, migrations, etc. I've inserted some example records like:

User.new(fullname: 'seb', ...).save
User.new(fullname: 'sab', ...).save
User.new(fullname: 'frank', ...).save

Activity.new(name: 'yoga', user_id: 1).save
Activity.new(name: 'pilates', user_id: 1).save
Activity.new(name: 'tennis', user_id: 2).save
Activity.new(name: 'etc etc etc', user_id: 3).save

Participation.new(user_id: 1, activity_id: 1).save
Participation.new(user_id: 2, activity_id: 1).save
Participation.new(user_id: 3, activity_id: 1).save

And I run the query, having 3 users that have participated in the activity with id 3:

User.joins(:participations).where('participations.activity_id = 1').pluck(:fullname)

And I get:

SELECT "users"."fullname" FROM "users" INNER JOIN "participations" ON "participations"."user_id" = "users"."id" WHERE (participations.activity_id = 1)
# => ["seb", "sab", "frank"]