Ruby on rails 轨道3 HABTM一衬板
我需要一个一行程序来生成一个has_,并且_属于_-many联接表,否则我将返回Django,以获得更简单的多对多构造 rails 3生成模型文章标签[…]Ruby on rails 轨道3 HABTM一衬板,ruby-on-rails,ruby-on-rails-3,many-to-many,model-associations,has-and-belongs-to-many,Ruby On Rails,Ruby On Rails 3,Many To Many,Model Associations,Has And Belongs To Many,我需要一个一行程序来生成一个has_,并且_属于_-many联接表,否则我将返回Django,以获得更简单的多对多构造 rails 3生成模型文章标签[…] Models # article.rb has_many :articles_tags has_many :tags, :through => :articles_tags # tag.rb has_many :articles_tags has_many :articles, :through => :articles_t
Models
# article.rb
has_many :articles_tags
has_many :tags, :through => :articles_tags
# tag.rb
has_many :articles_tags
has_many :articles, :through => :articles_tags
# article_tag.rb
belongs_to :tag
belongs_to :article
听起来你在寻找的标准有很多:
# article.rb
has_and_belongs_to_many :tags
# tag.rb
has_and_belongs_to_many :articles
您的联接表将被称为articles\u-tags
,只需要包含两列,article\u-id
和tag\u-id
(不需要id
列,因为它不是模型)
这是我的房间。我强烈建议您熟悉Rails指南
这对发电机来说太简单了。您只需要两个空模型类和联接表,它们将在如下迁移中定义:
def self.up
create_table :articles_tags, :id => false do |t|
t.integer :article_id
t.integer :tag_id
end
end
def self.down
drop_table :articles_tags
end
听起来你在寻找的标准有很多:
# article.rb
has_and_belongs_to_many :tags
# tag.rb
has_and_belongs_to_many :articles
您的联接表将被称为articles\u-tags
,只需要包含两列,article\u-id
和tag\u-id
(不需要id
列,因为它不是模型)
这是我的房间。我强烈建议您熟悉Rails指南
这对发电机来说太简单了。您只需要两个空模型类和联接表,它们将在如下迁移中定义:
def self.up
create_table :articles_tags, :id => false do |t|
t.integer :article_id
t.integer :tag_id
end
end
def self.down
drop_table :articles_tags
end
哦,等等,哈哈,我想这可能会奏效:
rails g model articles_tags article:references tag:references --no-id --no-timestamps
我想知道是否有任何方法来抑制模型文件的创建(article_tags.rb),这样我就可以只使用标准has_和_-belish_-to_许多语法,而不必指定:through参数?我正在寻找最终的一行程序:向任何能够改进上述一行程序的人提供提示,以启用仅使用has\u和\u belies\u许多语法,而不使用联接模型!否则,我将回到Django,它内置了许多字段。哦,等等,哈哈,我想这可能会奏效:
rails g model articles_tags article:references tag:references --no-id --no-timestamps
我想知道是否有任何方法来抑制模型文件的创建(article_tags.rb),这样我就可以只使用标准has_和_-belish_-to_许多语法,而不必指定:through参数?我正在寻找最终的一行程序:向任何能够改进上述一行程序的人提供提示,以启用仅使用has\u和\u belies\u许多语法,而不使用联接模型!否则,我将回到Django,它内置了许多字段。使用generate将创建两个索引
使用generate检查此答案将创建两个索引
检查这个答案我不知道经过这么长时间后,这是否仍然值得关注,但我认为您可以看看:它是一个Rails生成器,为HABTM表生成正确的迁移 我不知道经过这么长时间后,这是否仍然值得关注,但我想您可以看看:它是一个Rails生成器,为HABTM表生成正确的迁移