Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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
Activerecord Rails4复杂嵌套有“许多:通过,需要帮助才能找到”;轨道;_Activerecord_Ruby On Rails 4_Has Many Through - Fatal编程技术网

Activerecord Rails4复杂嵌套有“许多:通过,需要帮助才能找到”;轨道;

Activerecord Rails4复杂嵌套有“许多:通过,需要帮助才能找到”;轨道;,activerecord,ruby-on-rails-4,has-many-through,Activerecord,Ruby On Rails 4,Has Many Through,我的目标是让用户能够提名客人来谈论某个话题,但复杂的是,他们(或用户)可以提供许多“链接”,在提名客人时支持这种选择 因此,我的表格,以其最简单的形式,如下所示: create_table "topics", force: true do |t| t.string "name" end create_table "guests", force: true do |t| t.string "name" end create_table "topic_guests", for

我的目标是让用户能够提名客人来谈论某个话题,但复杂的是,他们(或用户)可以提供许多“链接”,在提名客人时支持这种选择

因此,我的表格,以其最简单的形式,如下所示:

create_table "topics", force: true do |t|
    t.string   "name"
end
create_table "guests", force: true do |t|
    t.string   "name"
end
create_table "topic_guests", force: true do |t|
    t.integer  "topic_id"
    t.integer  "guest_id"
    t.integer  "user_id" #(who nominated this guest)
end
create_table "links", force: true do |t|
    t.integer  "user_id"
    t.integer  "topic_id"
    t.integer  "guest_id"
    t.string   "url"
end
我很难让链接与has\u many:through一起工作,因为:through是另一个has\u many:through(whew)的连接表

添加主题和来宾非常适合此配置:

class Topic < ActiveRecord::Base

  belongs_to :user

  has_many :topic_guests, :dependent => :destroy  
  has_many :guests, :through => :topic_guests

end

class Guest < ActiveRecord::Base
  has_many :topic_guests, :dependent => :destroy  
  has_many :topics, :through => :topic_guests
end
类主题:破坏
有很多:客人,:通过=>:topic\u客人
结束
类Guest:破坏
有很多:主题,:至=>:主题
结束
在向指定的客人添加链接时,链接模型很简单:

class Link < ActiveRecord::Base
  belongs_to :guest
  belongs_to :topic
  belongs_to :user
end
class链接
但我似乎无法添加到指定客人的链接。我们的目标是做类似的事情(输入这个让我意识到为什么这是一个很难用“rails方式”完成的任务)


@topic.guests.find(params[:guest_id]).links我真的建议你坐下来好好想想模型之间的关系。有时,在纸上绘制数据库有助于了解模型的交互方式。有很多方法可以重组现有的数据。由于它实际上有点复杂,我将列出一些选项

使您的链接具有多态性

这似乎是第一个跳出来给我的,你应该使你的链接多态关系。这里有一个railscast(惊人的资源)

使用您的模型,您可以通过几种方式进行设置。例如,来宾和用户可以有许多多态链接,并且链接始终有一个主题id。这取决于您希望如何处理数据。迁移和模型类似于下面的内容

class CreateLinks < ActiveRecord::Migration
  def change
    create_table :links do |t|
      t.references :authorable, index: true  # ex: user_id or guest_id
      t.string :authorable_type  # ex: user or guest

      t.references :topic, index: true

      t.string :url

      t.timestamps
    end
  end
end
class CreateLinks
模型看起来像这样

link.rb

class Link < ActiveRecord::Base
  belongs_to :authorable, :polymorphic => true

  belongs_to :topic
end
class链接true
属于:主题
结束
topic.rb

class Topic < ActiveRecord::Base
  has_many :links
end
类主题
user.rb

class User < ActiveRecord::Base
  has_many :links, :as => :authorable
end
class用户:authorable
结束
guest.rb

class Guest < ActiveRecord::Base
  has_many :links, :as => :authorable
end
classguest:authorable
结束
这只是一个例子,你可以简单地做相反的事情,说链接属于用户,并使他们链接的内容具有多态性。这是我的下一点

您可以将来宾变成用户

这不是一项要求,但它可以帮助您将来宾全部移除,并使他们成为用户角色。根据您在这里提供的内容,为了简单起见,我会将客人分开,并使用一个与客人和用户有着千丝万缕关系的表,内表称为提名表

之所以使用has MULTY THORT,是因为您可能希望保存该关系的数据,例如,它可能有一个名为SUCCESS的布尔字段,当他被成功提名时,该字段将变为true。这里有一个很好的链接,解释了很多

有很多功能,在这里给你很大的灵活性

在这个特殊的例子中,你可能想使用has many to的另一个原因是,你可以说提名属于一个主题,而主题有很多提名。我认为这是一种很好的关联数据的方式,但这实际上取决于数据之间更深层次的交互,而您比我更了解这些交互

此应用程序中是否仅使用与名称和主题相关的链接?那么,客人只能被提名一个主题吗?这些链接也可以用于多个主题吗?这些都是你需要问自己的好问题。我只能提出一个建议,但这假设你会比我了解更多的东西,因为你更了解这个应用程序

我可以继续讨论应用程序对数据结构的影响程度,但以下是我将使用的数据之间的最终联系:

  • 用户通过提名有很多客人
  • 嘉宾通过提名有很多用户
  • 提名属于一个主题
  • 主题有很多提名
  • 提名有很多联系
  • 链接属于提名
  • 链接与创建它们的人是多态的(在我的示例中是可编写的)
  • 我认为这足以解决你的问题。祝你好运

    class Guest < ActiveRecord::Base
      has_many :links, :as => :authorable
    end