Ruby on rails Mongoid拥有并且属于许多没有'_ids';后缀
在以下示例中:Ruby on rails Mongoid拥有并且属于许多没有'_ids';后缀,ruby-on-rails,mongodb,mongoid,mongoid4,Ruby On Rails,Mongodb,Mongoid,Mongoid4,在以下示例中: class Band include Mongoid::Document has_and_belongs_to_many :tags end class Tag include Mongoid::Document field :name, type: String has_and_belongs_to_many :bands end 对象的存储方式如下: # The band document. { "_id" : ObjectId("4d3ed089
class Band
include Mongoid::Document
has_and_belongs_to_many :tags
end
class Tag
include Mongoid::Document
field :name, type: String
has_and_belongs_to_many :bands
end
对象的存储方式如下:
# The band document.
{
"_id" : ObjectId("4d3ed089fb60ab534684b7e9"),
"tag_ids" : [ ObjectId("4d3ed089fb60ab534684b7f2") ]
}
# The tag document.
{
"_id" : ObjectId("4d3ed089fb60ab534684b7f2"),
"band_ids" : [ ObjectId("4d3ed089fb60ab534684b7e9") ]
}
是否可以将字段
tag\u id
重命名为tags
,band\u id
重命名为bands
?谢谢您可以使用以下语法来使用特定的外键名:
class Band
include Mongoid::Document
has_and_belongs_to_many :tags, foreign_key: "bands"
end
class Tag
include Mongoid::Document
field :name, type: String
has_and_belongs_to_many :bands, foreign_key: "tags"
end
但避免使用可能与可用关系冲突的外键名称。我有一个现有数据库。