如何将params传递给模型查询

时间:2015-07-25 10:41:28

标签: ruby-on-rails

我有一个简单的应用程序,1个用户通过Subscription表(具有user_id和challenge_id字段)与挑战有多对多的关系。 1个用户也有许多关系(列follower_id和followed_id)。

我正在尝试在我的挑战模型中创建一个查询,它允许我检索属于特定挑战的所有用户及其关系。从我看来,挑战已经在params中通过了。我还应该提一下,查询必须以特定的json格式返回,如下所示。

查询应该说:选择所有用户及其关系,其challenge_id与params Challenge.find(params [:id])

相同

我的问题是如何使用param中传递的质询的id来在我的模型中执行查询。我也意识到我并不真正知道如何查询多对多的关系。我在下面试过,但我觉得它很乱。我想我在这里尝试做的是让用户接受来自params的挑战,然后将用户ID和关系字段添加到数组中。

我期待的输出是:

{"nodes":["1","2","3"],"edges":[["1","3"],["2","3"],["2","1"]]}

模型及其关系如下 - 我知道它有点正在进行中,但相关的协会在那里:

enter image description here

模特:

#user.rb
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
         acts_as_commontator
         acts_as_messageable

  has_many :microposts, dependent: :destroy
  has_many :subscribers
  has_many :challenges, through: :subscribers
  has_many :relationships, foreign_key: "follower_id", dependent: :destroy
  has_many :followed_users, through: :relationships, source: :followed
  has_many :reverse_relationships, foreign_key: "followed_id", class_name:  "Relationship", dependent:   :destroy
  has_many :followers, through: :reverse_relationships, source: :follower
  has_many :posts

  has_many :active_relationships, class_name:  "Relationship",
    foreign_key: "follower_id",
    dependent:   :destroy
  has_many :following, through: :active_relationships, source: :followed
  has_many :passive_relationships, class_name:  "Relationship",
    foreign_key: "followed_id",
    dependent:   :destroy
  has_many :followers, through: :passive_relationships, source: :follower

  has_many :challenges

  def reciprocal_followers
    self.followers & self.followed_users
  end

  #For D3
  # def self.including_relationships
  #   result={}
  #   result["nodes"]=User.select(:name, :group, :id).map{|u| {name: u.name, group: u.group, id: u.id} }
  #   result["links"]=Relationship.select('follower_id as source, followed_id as target, value').map{|x| {source: x.source, target: x.target, value: x.value} }
  #   result
  # end

  def self.including_relationships
    result={}
    result["nodes"] = User.all.map {|u| u.id.to_s}
    result["edges"] = Relationship.all.map { |r| [r.follower_id.to_s, r.followed_id.to_s] } 
    result
  end

  #To add scores together
  def overall_score
    self.FBScore + self.PIScore
  end

  def mailboxer_email(object)
    email
  end

  def follow(other_user)
    active_relationships.create(followed_id: other_user.id)
  end

  # Unfollows a user.
  def unfollow(other_user)
    active_relationships.find_by(followed_id: other_user.id).destroy
  end

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

subscriber.rb

class Subscriber < ActiveRecord::Base
    belongs_to :user
    belongs_to :challenge
end

class Relationship < ActiveRecord::Base
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"
  validates :follower_id, presence: true
  validates :followed_id, presence: true
end

challenge.rb

class Challenge < ActiveRecord::Base
    has_many :entries
    has_many :users, through: :subscribers
    has_many :subscribers
    belongs_to :user
end

这是我无法弄清楚的部分:

def self.including_relationships

  users.subscribers.includes(Challenge.find(params[:id])).each do |user|
    result={}
    result["nodes"] = User.all.map {|u| u.id.to_s}
    result["edges"] = Relationship.all.map { |r| [r.follower_id.to_s, r.followed_id.to_s] } 
    result
  end

end

使用@ Athar的代码进行更新 - 尽管目前这给我错误的参数数量(0表示1)错误

Challenge.rb

def self.joinup(id)
  c = Challenge.find(id)
  result={}
  user_ids  =  c.users.pluck(:id)
  result["nodes"] = user_ids.collect(&:to_s)
  result["edges"] = Relationship.where(follower_id: user_ids).map{|h| [h.follower_id.to_s, h.followed_id.to_s]}
  result
 end

challenge_controller.rb

def join
     @challenge = Challenge.find(params[:id])
     @challenge.users << current_user
     @users = Challenge.joinup
         respond_to do |format|
         format.html # index.html.erb
         format.json {  render json: @users } 
     end
     #Actually I want to redirect through to create json from a node edge query in the model based on the users who have joined up to that challenge.
     #So firstly I need to create the model to query the database and produce the json structure....Where do I do this ?in challenges model or can I use the users model.
     #Then I need to produce json in this controller action using similar to user controller index.
     #Then I need to render the view in a my challenges page.
  end

1 个答案:

答案 0 :(得分:1)

这会有所帮助。

请将此方法添加到任何模型中。我会建议challenge模型,并从控制器将id传递给模型。

def self.including_relationships(id)
  c = Challenge.find(id)
  result={}
  user_ids  =  c.users.pluck(:id)
  result["nodes"] = user_ids.collect(&:to_s)
  result["edges"] = Relationship.where(follower_id: user_ids).map{|h| [h.follower_id.to_s, h.followed_id.to_s]}
  result
end