Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 设置和查询三个模型之间的ActiveRecords关联_Sql_Ruby On Rails_Activerecord_Associations_Model Associations - Fatal编程技术网

Sql 设置和查询三个模型之间的ActiveRecords关联

Sql 设置和查询三个模型之间的ActiveRecords关联,sql,ruby-on-rails,activerecord,associations,model-associations,Sql,Ruby On Rails,Activerecord,Associations,Model Associations,我是一个新出炉的Rails“开发者”,这是我的第一个StackOverflow问题(耶!)。我正在构建一个应用程序(用于我的期末项目),允许用户通过预测给定电视节目上的活动和事件来获得积分。到目前为止,我很开心,但是我很难找到定义以下模型之间关联的最佳方法 我已经研究了三个模型之间的三向关联,但大多数答案(如和)并没有解决所有三个模型之间的HABTM关联。。。非常感谢您的帮助 我的问题概述: 一个节目由许多集组成,每个节目都有许多参赛者(角色)。通过节目的关联,参赛者和剧集应该有一个HABTM关

我是一个新出炉的Rails“开发者”,这是我的第一个StackOverflow问题(耶!)。我正在构建一个应用程序(用于我的期末项目),允许用户通过预测给定电视节目上的活动和事件来获得积分。到目前为止,我很开心,但是我很难找到定义以下模型之间关联的最佳方法

我已经研究了三个模型之间的三向关联,但大多数答案(如和)并没有解决所有三个模型之间的HABTM关联。。。非常感谢您的帮助

我的问题概述: 一个节目由许多集组成,每个节目都有许多参赛者(角色)。通过节目的关联,参赛者和剧集应该有一个HABTM关系,但我的应用程序不需要这个部分

棘手的是,每个参赛者可以在给定的插曲中获得许多分。我想做的是记录每个参赛者每集获得的分数,然后在我需要为用户计算分数时查询

我尝试了一个属性为插曲id作为第三列的连接表参赛者积分,但这不起作用。我也曾玩弄过一个表格参赛者\u集\u点数的想法,但我不知道如何为该表格创建一个新实例。以下是我对构建以下模型的尝试:

参赛者.rb

class Contestant < ActiveRecord::Base
    belongs_to :show
    has_and_belongs_to_many :points, inverse_of: :contestants
    has_many :episodes, through: :points                         # not sure about this
end
class Episode < ActiveRecord::Base
    belongs_to :show
    has_and_belongs_to_many :points, inverse_of: :episodes
    has_many :contestants, through: :points                    # not sure about this
end
class Point < ActiveRecord::Base
    belongs_to :show
    has_and_belongs_to_many :contestants, inverse_of: :points
    before_destroy { contestants.clear }
    has_and_belongs_to_many :episodes, inverse_of: :points    
end
班级参赛者
示例:参赛者1={姓名:'Gloria',年龄:30岁,职业:'护士,show_id:1}

插曲.rb

class Contestant < ActiveRecord::Base
    belongs_to :show
    has_and_belongs_to_many :points, inverse_of: :contestants
    has_many :episodes, through: :points                         # not sure about this
end
class Episode < ActiveRecord::Base
    belongs_to :show
    has_and_belongs_to_many :points, inverse_of: :episodes
    has_many :contestants, through: :points                    # not sure about this
end
class Point < ActiveRecord::Base
    belongs_to :show
    has_and_belongs_to_many :contestants, inverse_of: :points
    before_destroy { contestants.clear }
    has_and_belongs_to_many :episodes, inverse_of: :points    
end
class插曲
示例:第1集={show_id:1,air_date:2014/04/02}

point.rb

class Contestant < ActiveRecord::Base
    belongs_to :show
    has_and_belongs_to_many :points, inverse_of: :contestants
    has_many :episodes, through: :points                         # not sure about this
end
class Episode < ActiveRecord::Base
    belongs_to :show
    has_and_belongs_to_many :points, inverse_of: :episodes
    has_many :contestants, through: :points                    # not sure about this
end
class Point < ActiveRecord::Base
    belongs_to :show
    has_and_belongs_to_many :contestants, inverse_of: :points
    before_destroy { contestants.clear }
    has_and_belongs_to_many :episodes, inverse_of: :points    
end
类点
示例:第1点={event:'参赛者进入一个参数',show_id:1,points:10}

示例:第2点={事件:“选手在第7集被淘汰”,节目编号:1,分:70}

大难题: 我应该如何在所有三个模型之间建立关联,以便查询:

  • 选手A在第一集中得了多少分
  • 哪些选手在第一集中得分
  • 参赛者在第1-5集之间得了多少分

HABTM是获得我想要的查询结果的正确方法吗?有没有更简单的方法来构建这三个模型?再次感谢你的帮助

听起来你需要一集才能通过该集的参赛者获得很多分数

参赛者有分数,剧集有参赛者。从一集到该集点数的方法是通过参赛者


希望这会有所帮助。

我正在浏览本集->参赛者->积分示例,以使答案简短明了

首先是一些建议:当您在处理activerecords时遇到任何问题时,请跳转到
rails控制台

现在您已经提到,参赛者
属于:show
也属于剧集
属于:show

为了支持这些关系,您必须在
参赛者
剧集
表中同时显示
show_id
。这就是rails维护关联的方式。因此,每次你创建一个参赛者或一集,你也会与他们一起传递节目id

假设show id为1。 那么

现在访问该节目的所有参赛者或剧集:

show.contestants 
# => returns all contestants for show 1
show.episodes
# => returns all episodes for show 1
现在回到你的问题:

最棘手的是,每个参赛者都可以在一次比赛中获得很多分 给定的一集。我想做的是记录每一个点 参赛者每集的收入,然后在我需要的时候查询 为用户计算分数

如果我是正确的,分数由参赛者打分。所以你的关系不正确: 点
属于:contenstant

向前看,由于分数属于参赛者,所以在
积分表中会有“参赛者id”列,这也是保持关系的方式

因此,让我们创建一个参赛者

contestant = Contestant.create(name, attributes, show_id, episode_id) # as episode has many contestant 
# => returns contestant
现在创建一集

episode = Episode.create(name, attributes, show_id)
# => returns episode
要查看本集的所有参赛者,请执行以下操作:

episode.contestants
为参赛者创造积分:

point = Point.create(value, contestant_id)
参赛者的得分

contenstant.points 
现在回到正题

point.contestant
#=> returns contestant to whom it belong

point.contestant.episode 
#=> returns episode in which it was scored

point.contestant.show 
#=> returns the show that point belongs to
PS:这是一个很长的答案,所以即使我试图保持专注,我也可能在两者之间迷失了方向,如果这样问的话,我会澄清具体的问题。

当前的解决方案 我的一位出色的同学建议我将点视为事件的实例,并将点类重命名为事件,然后创建新的类,其属性为参赛者id、插曲id事件id

class Event < ActiveRecord::Base
    belongs_to :franchise, inverse_of: :events
    has_many :points
    has_many :contestants, through: :points
    has_many :episodes, through: :points
end

class Point < ActiveRecord::Base
    belongs_to :contestant
    belongs_to :episode
    belongs_to :event
end

现在我可以轻松访问实际的点分配。非常容易重新构造,只需对我的其他10款车型进行最少的更新。谢谢徐鹏飞

可能是重复的谢谢,布拉德。一旦我很好地建立了模型,上面的答案肯定会帮助我进行查询,但我不清楚我应该如何构建模型和关联。。。我会继续研究的!您应该将解决方案作为答案发布,而不是将其附加到您的问题(然后