モンゴイドを使用して、ネストされた属性を持つフォームを作成しようとしています。私のモデルには次のコードがあります。
def Company
field :name
has_many :users, autosave: true, dependent: :destroy
accepts_nested_attributes_for :users
end
def User
belongs_to :company
has_one :profile
end
def Profile
belongs_to :user
end
フォームから返されるパラメーターは次の順序です。
"company"=>
{"users_attributes"=>
{"0"=>
{"profile_attributes"=>
{"first_name"=>"123123abcd123", "last_name"=>"abcd123123123"},
"email"=>"abcd@abcd.com123123123",
"password"=>"123123123123",
"password_confirmation"=>"123123123123"}},
"name"=>"abcd123123123",
"subdomain"=>"abcd123123123"}
Company.create(params[:company]) の呼び出しは機能しているようですが、ユーザー オブジェクトが適切に作成されていません。company.users を実行すると、そのオブジェクトが表示されますが、User.find を実行すると、そのドキュメントは利用できません。ドキュメントを読んで、次の方法でパラメーターを渡す必要があることに気付きました。
"company"=>
{"users_attributes"=>
[{"profile_attributes"=>
{"first_name"=>"123123123", "last_name"=>"123123123"},
"email"=>"testin321@gmail.com",
"password"=>"123123",
"password_confirmation"=>"123123"}],
"name"=>"abcd123123123",
"subdomain"=>"abcd123123123"}
ハッシュの代わりに users_attributes に配列を使用するという微妙な違いに注意してください。これは正しく機能しますが、Active Record の場合のように (そしてレールのようなものでどのようにすべきか) まったく独創的ではないように見えます。params ハッシュを取得してデータを変更して、特定の規則に従うようにしたくありません。より良い方法はありますか、何か不足していますか?