【问题标题】:Attach model has_and_belongs_to_many relationship in rails在 rails 中附加模型 has_and_belongs_to_many 关系
【发布时间】:2013-08-20 15:09:32
【问题描述】:

我有两个模型 PostPostCategory 两者都有 has_and_belongs_to_many 关系。

我手动创建了三个post_categoriesnewsdon't missannouncements

当我创建一个新的post 时,我希望能够通过selectmultiple="true" 选择其中的categories belongs to

注意:我不想在post 创建时创建一个新的category,而是将一个或多个categories 附加到这个post

【问题讨论】:

    标签: ruby-on-rails ruby relationship has-many has-and-belongs-to-many


    【解决方案1】:

    你可以这样做:

    form.select :category_ids, PostCategory.all.map{ |pc| [pc.name_or_smth_else, pc.id] }, {}, {:multiple => true}
    

    看看guide的“4.4.1.8 collection_singular_ids”部分

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多