【问题标题】:Rails many to many (has_many :through) - how can a form add the children?Rails 多对多(has_many :through) - 表单如何添加孩子?
【发布时间】:2013-12-24 22:41:00
【问题描述】:

我在用户表和帐户表之间存在多对多关系,使用 :has_many, :through 方法实现。用户可以有多个帐户,帐户可以有多个用户。

我有一个表单,用户可以在其中选择他想要关联的所有帐户。我想将帐户关联到 User 对象,以便我可以查询 @user.accounts 并获得更新的列表。

class User
   has_many :accountings
   has_many :accounts, :through => :accountings

class Neighborhood   
   has_many :accountings   
   has_many :users, :through => :accountings

class Accountings
   belongs_to :user
   belongs_to :accountings

我应该如何实现发送帐户 ID 和更新数据库的表单?

我觉得这可能是一个基本问题,但我正在寻找 Rails 做事的方式。提前致谢。

【问题讨论】:

标签: ruby-on-rails forms activerecord associations has-many-through


【解决方案1】:

使用以下复选框解决了这个问题(角度):

    <input type="checkbox" name="user[account_ids][]"
    value="{{ hood.id }}">{{ hood.name }} </div>

这将更新模型的多对多关系,以便我可以查询 user.account_ids 并获取更新的列表。

【讨论】:

    猜你喜欢
    • 2011-06-06
    • 2011-10-07
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    相关资源
    最近更新 更多