[英]How to pass params to a model query
我有一個簡單的應用程序,其中有1個用戶通過Subscription表(具有user_id和Challenge_id字段)與挑戰有很多關系。 1個用戶還具有許多關系(具有follower_id和followed_id列)。
我試圖在我的挑戰模型中創建一個查詢,該查詢允許我檢索作為特定挑戰一部分的所有用戶及其關系。 我認為挑戰已經在參數上傳遞了。 我還應該提到查詢必須以特定的json格式返回,如下所示。
該查詢應說:選擇所有具有與參數Challenge.find(params [:id])相同的Challenge_id的用戶及其關系
我的問題是如何使用在參數中傳遞的質詢的ID在我的模型中執行查詢。 我也剛剛意識到我並不真正知道如何查詢多對多關系。 我在下面嘗試過,但是我覺得很混亂。 我想在這里要做的是從參數中獲取與挑戰有關的用戶,然后將用戶ID和關系字段添加到數組中。
我期望的輸出是這樣的:
{"nodes":["1","2","3"],"edges":[["1","3"],["2","3"],["2","1"]]}
這些模型及其關系如下:我知道它尚在開發中,但相關的聯系在那兒:
型號:
#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
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
這會有所幫助。
請將此方法添加到任何模型中。 我建議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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.