【问题标题】:has_many :through railshas_many : 直通铁轨
【发布时间】:2015-10-23 05:10:23
【问题描述】:

我想让 user1 接受其他用户的请求,以作为贡献者加入 user1 的帖子,如果 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 迁移

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 迁移

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

创建组迁移

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>

有什么建议吗?

【问题讨论】:

  • 你的问题是什么?不清楚
  • 我不确定我的方向是否正确 > 想要让很多用户加入一个帖子
  • 当我在控制台中时:当我输入 >> p.user_ids 时我没有得到任何回报
  • 我认为你的模型是正确的。尝试在控制台中输入p.users(p 是帖子),您应该有同组用户列表。

标签: ruby activerecord associations


【解决方案1】:

你可以试试这个---

因为我认为 p 是您的帖子,并且您想获取所有 user_id。

   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)

我觉得应该对你有帮助

【讨论】:

  • 尝试添加组类 - has_many: users , :foreign_key => "accepted_id", :class_name => "User" ---------------- --- 并在控制台中运行 p.users 如果它不起作用那么请给我控制台的错误消息
  • 我得到 >> 未定义的方法 `requester' for nil:NilClass >> @users = @user.requester.paginate(page: params[:page]) ?
  • 您的错误在于您计算用户的请求者方法中的后控制器 - 这是实例变量,因此默认其值为 nil 您需要计算用户。
  • 如需进一步帮助,请向我展示 params 中的内容以及您的迁移后内容。否则你可以通过 session[:user_id] 找到当前用户 ID
  • 刚刚更新了参数和迁移@ArvindKumar
猜你喜欢
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-06
  • 2016-08-18
  • 2015-12-28
相关资源
最近更新 更多