Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 rails4//在添加关系时通过语法有很多_Ruby On Rails 4_Syntax_Associations_Has Many - Fatal编程技术网

Ruby on rails 4 rails4//在添加关系时通过语法有很多

Ruby on rails 4 rails4//在添加关系时通过语法有很多,ruby-on-rails-4,syntax,associations,has-many,Ruby On Rails 4,Syntax,Associations,Has Many,如何将参数传递给has\u many关联 假设我们有类别和帖子,与一个联接表关联,category_posts category has_many category_posts category_has_many posts, through category_posts 和文章的相反方向(不是正确的语法,为了让它在这里快速) 假设我们得到了添加以下内容的基本语法: category.posts << post category.posts您必须直接添加到category\u p

如何将参数传递给has\u many关联

假设我们有类别和帖子,与一个联接表关联,category_posts

category has_many category_posts
category_has_many posts, through category_posts
和文章的相反方向(不是正确的语法,为了让它在这里快速)

假设我们得到了添加以下内容的基本语法:

category.posts << post

category.posts您必须直接添加到
category\u posts
关系中,例如:

category.category_posts << CategoryPost.create(post: post, other_field: other_value)
category.category_posts.create(post: post, other_field: other_value)