108得票3回答
如何在Rails中向has_many :through关联添加记录

class Agents << ActiveRecord::Base belongs_to :customer belongs_to :house end class Customer << ActiveRecord::Base has_many :age...

7得票2回答
动态创建has_many或habtm关联的after_add和after_remove回调?

有没有办法动态地向现有的has_many或has_and_belongs_to_many关系中添加after_add和after_remove回调函数? 例如,假设我有模型User、Thing和一个连接模型UserThingRelationship,并且User模型如下: class Us...

15得票3回答
使用 has_many :through 和 build

我有三个模型,都用于 has_many :through 关联。它们的结构如下:class Company < ActiveRecord::Base has_many :company_users, dependent: :destroy has_many :users, th...

18得票1回答
用户控制器#欢迎页面中出现了ActiveRecord::HasManyThroughAssociationNotFoundError错误

我在Rails中有一个多对多的关联。所有数据库表都按照相应和适当的方式命名。所有模型文件都是复数形式,并使用下划线来分隔单词。所有的命名规范都遵循了Ruby和Rails的标准。我在我的模型中使用has many through,就像这样:has_many :users, :through =&...

44得票3回答
Rails: HasManyThroughAssociationNotFoundError

我遇到了一个问题,无法使has_many through关联起作用。 我一直收到这个异常: Article.find(1).warehouses.build ActiveRecord::HasManyThroughAssociationNotFoundError: Could not fi...

10得票1回答
如何使用has_many :through并遵守:conditions创建新记录?

假设我有一门课程,学生可以通过会员资格(例如Courses和Students之间的has_and_belongs_to_many关系)进行注册。一些会员是供观察班级的学生使用的(不计入学分等),因此:class Course < ActiveRecord::Base has_many...

10得票2回答
多对多关系中的多态HasManyThrough关系

在我的 Laravel 应用程序中,我有以下类:class Product extends Model { public function extended() { return $this->morphTo(); } public fu...

9得票3回答
在Rails中使用formtastic与一个关联模型(has_many)的字段

我搜索并尝试了很多方法,但无法如愿达成。以下是我的问题。 class Moving < ActiveRecord::Base has_many :movingresources, :dependent => :destroy has_many :resources, :t...

14得票4回答
Rails:为什么“has_many ...,:through => ...”关联会导致“NameError:未初始化常量...”错误?

为了表达一个组可以有多个用户,而一个用户也可以属于多个组,我设置了以下关联:class Group < ActiveRecord::Base has_many :users_groups has_many :users, :through => :users_groups ...

7得票1回答
Rails 4: 结合has_many :through关联和多态关联

在我的Rails 4应用程序中,我有以下模型: User has_many :administrations has_many :calendars, through: :administrations has_many :comments has_many :calendar_commen...