Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.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
Ruby on rails RubyonRails:关联_Ruby On Rails_Ruby On Rails 3_Activerecord_Associations - Fatal编程技术网

Ruby on rails RubyonRails:关联

Ruby on rails RubyonRails:关联,ruby-on-rails,ruby-on-rails-3,activerecord,associations,Ruby On Rails,Ruby On Rails 3,Activerecord,Associations,我是ROR的新手,所以我现在被这个问题困住了 我正在用文章和模板设置一个应用程序。 我想用许多文章创建模板,相同的文章可以分配给几个不同的模板。 所以,当你创建一个模板,我想分配文章给它。你会认为直接使用a有很多联系,但我不认为这抓住了我的问题 我想把文章分配给多个模板,所以我想在这里使用某种链接表 但我不知道如何在rails中实现它!或者我应该寻找什么样的解决方案 有没有人可以就这个问题向我提供建议?您是否正在寻找是否有多个协会 您是否正在寻找是否有多个协会 尝试在上查看资料 基本上,转到控制

我是ROR的新手,所以我现在被这个问题困住了

我正在用文章和模板设置一个应用程序。 我想用许多文章创建模板,相同的文章可以分配给几个不同的模板。 所以,当你创建一个模板,我想分配文章给它。你会认为直接使用a有很多联系,但我不认为这抓住了我的问题

我想把文章分配给多个模板,所以我想在这里使用某种链接表

但我不知道如何在rails中实现它!或者我应该寻找什么样的解决方案


有没有人可以就这个问题向我提供建议?

您是否正在寻找
是否有多个
协会


您是否正在寻找
是否有多个
协会


尝试在上查看资料

基本上,转到控制台并键入

$rails g模型文章标题:字符串正文:文本

$rails g模型模板名称:字符串一些其他属性:类型等

$rails g迁移创建文章模板

然后使用以下内容编辑“创建文章”模板:

class CreateArticlesTemplates < ActiveRecord::Migration
  def up
    create_table :articles_templates, :id => false do |t|
      t.integer :template_id, :article_id
    end
  end

  def down
  end
end
class CreateArticlesTemplatesfalse do | t|
t、 整数:模板\u id,:文章\u id
结束
结束
降下
结束
结束

尝试在上签出内容

基本上,转到控制台并键入

$rails g模型文章标题:字符串正文:文本

$rails g模型模板名称:字符串一些其他属性:类型等

$rails g迁移创建文章模板

然后使用以下内容编辑“创建文章”模板:

class CreateArticlesTemplates < ActiveRecord::Migration
  def up
    create_table :articles_templates, :id => false do |t|
      t.integer :template_id, :article_id
    end
  end

  def down
  end
end
class CreateArticlesTemplatesfalse do | t|
t、 整数:模板\u id,:文章\u id
结束
结束
降下
结束
结束

您可以创建链接模型文章\u模板

rails generate model articles_template
class ArticlesTemplate < ActiveRecord::Base
  belongs_to :article
  belongs_to :template
end

class Article < ActiveRecord::Base
  has_many :articles_templates
  has_many :templates, :through => :articles_templates
end

class Template < ActiveRecord::Base 
  has_many :articles_templates
  has_many :articles, :through => articles_templates
end
参考文章和模板

class CreateArticlesTemplates < ActiveRecord::Migration
  def change
    create_table :articles_templates do |t|
      t.references :article
      t.references :template
      t.timestamps
    end
  end
end
class CreateArticlesTemplates
然后在模型文章模板中设置关联

rails generate model articles_template
class ArticlesTemplate < ActiveRecord::Base
  belongs_to :article
  belongs_to :template
end

class Article < ActiveRecord::Base
  has_many :articles_templates
  has_many :templates, :through => :articles_templates
end

class Template < ActiveRecord::Base 
  has_many :articles_templates
  has_many :articles, :through => articles_templates
end
class-ArticlesTemplate:文章\u模板
结束
类模板文章\u模板
结束

这是最佳实践,因为您可以在链接模型和表中添加一些额外的功能。(更多信息)

您可以创建链接模型文章\u模板

rails generate model articles_template
class ArticlesTemplate < ActiveRecord::Base
  belongs_to :article
  belongs_to :template
end

class Article < ActiveRecord::Base
  has_many :articles_templates
  has_many :templates, :through => :articles_templates
end

class Template < ActiveRecord::Base 
  has_many :articles_templates
  has_many :articles, :through => articles_templates
end
参考文章和模板

class CreateArticlesTemplates < ActiveRecord::Migration
  def change
    create_table :articles_templates do |t|
      t.references :article
      t.references :template
      t.timestamps
    end
  end
end
class CreateArticlesTemplates
然后在模型文章模板中设置关联

rails generate model articles_template
class ArticlesTemplate < ActiveRecord::Base
  belongs_to :article
  belongs_to :template
end

class Article < ActiveRecord::Base
  has_many :articles_templates
  has_many :templates, :through => :articles_templates
end

class Template < ActiveRecord::Base 
  has_many :articles_templates
  has_many :articles, :through => articles_templates
end
class-ArticlesTemplate:文章\u模板
结束
类模板文章\u模板
结束

这是最佳实践,因为您可以在链接模型和表中添加一些额外的功能。(更多信息)

这也是一个很好的问题解决方案。最后,这真的取决于是否需要与两个模型的连接相关联的一些状态。这也是一个很好的解决问题的方法。最后,这实际上取决于是否需要将某些状态与两个模型的连接关联起来。