has_many:通过rails

时间:2015-10-23 05:10:23

标签: ruby activerecord associations

我想让user1接受来自其他用户的请求,以加入user1s帖子作为贡献者,并在user1接受时列出

似乎无法让它发挥作用 - 这就是我的设置:

has_many :through

我在想:

  

发布模型

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
  

用户模型

class User < ActiveRecord::Base

  #Join table associations
  belongs_to :group
  

组模型

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
  

DeviseCreateUsers Migration

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
  

后控制器

  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
  

展后展示

  <% @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>

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你可以试试这个---

因为我认为p是你的帖子,你想获得所有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)

我认为它应该对你有所帮助