【发布时间】:2014-07-22 22:35:25
【问题描述】:
我有一组如下所示的 Rails 模型,并在 Rails 4 中使用强大的参数通过钱包模型创建卡和用户之间的关联:
class User < ActiveRecord::Base
has_many :wallets
has_many :cards, through: :wallets
end
class Card < ActiveRecord::Base
has_many :wallets
has_many :users, through: :wallets
end
class Wallet < ActiveRecord::Base
belongs_to :user
belongs_to :card
end
在我的基础卡控制器中,我定义了如下强参数:
def card_params
params.require(:card).permit(:nickname, :number, :user_ids)
end
但在我的 API 卡控制器中,我想接受一个单一的 user_id 作为“user_id”键的值。如何设置 API::CardsController 类以通过 POST 接受 user_id 而不是(或除此之外) user_ids?
我的 API 卡控制器的要点:https://gist.github.com/tfilter/91986ae0ed6e215cbeca
【问题讨论】:
标签: ruby-on-rails-4 associations rails-activerecord