Tag: 已经拥有并且属于多个

Rails 3:设计添加角色复选框到注册表单

我正在使用Devise进行用户注册。 我一直在阅读有关定制Devise的所有着名教程 ,但无法理解这个简单的任务。 我跟着他的模特(HABTM) 我想在Devise编辑表单中添加一个角色复选框。 我没有控制器原因Devise没有提供,但设法为新用户添加默认角色。 我能够显示选中正确信息的复选框,但无法编辑它(它不会保存任何数据)。 我需要自定义控制器吗? 如果是,怎么样? 我是HABTM关系的新手! 我的用户模型 class User < ActiveRecord::Base has_and_belongs_to_many :roles before_save :setup_role # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, :lockable and :timeoutable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me def […]

如何在我的灯具中加载HABTM与外键关系?

我有以下两个模型:School和User,以及它们之间的HABTM关系,带有连接表。 在此连接表中,引用User表的外键不是user_id而是student_id 。 class School “User”, :join_table => “schools_students”, :foreign_key => “student_id” end class User “School”, :join_table => “schools_students”, :foreign_key => “school_id” end 我想在我的用户和学校设备中创建一个学校和一个用户,但在User中定义的foreign_key似乎是个问题。 fixtures/users.yml : user1: name: Student studying_schools: school1 fixtures/schools.yml : school1: name: School 1 active: true students: user1 加载上面的灯具会返回一个ActiveRecordexception: ActiveRecord::StatementInvalid: Mysql::Error: Unknown column ‘user_id’ in ‘field list’: INSERT INTO schools_students (student_id, user_id) […]

Rails:是否可以为has_and_belongs_to_many关联添加额外的属性?

我的意思是,如果我有两个模型,通过has_and_belongs_to_many关联连接,我可以在连接表中为每个关联存储其他数据吗? 也就是说,额外数据不会是任何一个表中单个记录的一部分,而是它们之间的连接。 我的实际模型如下: class Part true end class Package < ActiveRecord::Base has_and_belongs_to_many :parts belongs_to :user end 所以重点是每个部件都有许多包装,每个包装都有不同的部件。 我要添加的是数量。 这不是每个部件的数量,而是每个部件的每个包装的数量。 我在ActiveRecord中找不到如何做到这一点。 如果我没有使用rails / activerecord,我只需要在连接表中添加一个数量列,它将部件与包相关联。 我显然可以在迁移中进行此更改,但是如何使用ActiveRecord访问该值?