Has_many: through the rails

I want user1 to accept requests from other users in order to join user1s message as participants and be specified if user1 accepts them

It does not seem to work - here is my setup:

has_many :through 

I think:

post model

 class Post < ActiveRecord::Base #Join table associations has_many :group_requests, class_name: "Group", foreign_key: "requester_id", dependent: :destroy has_many :group_users, class_name: "Group", foreign_key: "accepted_id", dependent: :destroy has_many :requester, through: :group_requests has_many :accepted, through: :group_users def request(other_post) group_requests.create(accepted_id: other_post.id) end # Unfollows a user. def unrequest(other_post) group_requests.find_by(accepted_id: other_post.id).destroy end # Returns true if the current user is following the other user. def accepted?(other_post) requesting.include?(other_post) end 

user model

 class User < ActiveRecord::Base #Join table associations belongs_to :group 

group model

 class Group < ActiveRecord::Base belongs_to :requester, class_name: "Post" belongs_to :accepted, class_name: "Post" validates :requester_id, presence: true validates :accepted_id, presence: true end 

CreatePosts Migration

 class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :title t.text :content t.timestamps null: false end end end 

Migration DeviseCreateUsers

 class DeviseCreateUsers < ActiveRecord::Migration def change create_table(:users) do |t| ## Database authenticatable t.integer :post_id t.string :email, null: false, default: "" 

AddUserIdToPosts

 class AddUserIdToPosts < ActiveRecord::Migration def change add_column :posts, :user_id, :integer add_index :posts, :user_id end end 

CreateGroup Migration

 class CreateGroups < ActiveRecord::Migration def change create_table :groups do |t| t.integer :requester_id t.integer :accepted_id t.timestamps null: false end add_index :groups, :requester_id add_index :groups, :accepted_id add_index :groups, [:requester_id, :accepted_id], unique: true end end 

Mail controller

  def accepted @title = "Accepted" @post = Post.find(params[:id]) @users = @user.accepted.paginate(page: params[:page]) render 'show_accepted' end def requester @title = "Requesters" @post = Post.find(params[:id]) @users = @user.requester.paginate(page: params[:page]) render 'show_requester' end private def post_params params.require(:post).permit(:title, :content, :image) end 

Mail show

  <% @post ||= current_user(@post) %> <div class="stats"> <a href="<%= accepted_post_path(@post) %>"> <strong id="following" class="stat"> <%= @post.accepted.count %> </strong> following </a> <a href="<%= requester_post_path(@post) %>"> <strong id="followers" class="stat"> <%= @post.requester.count %> </strong> followers </a> </div> 

Any suggestions?

0
source share
1 answer

You can try this ---

I think p is your post and you want to get all user_ids.

  p.group_members.map{|gm|gm.user_id} #list of all ids in array ### OR try this p.users #list of all users in array #if you want to collect ids then p.users.collect(&:id) 

I think this should be useful for you.

+1
source

Source: https://habr.com/ru/post/1397959/


All Articles