好的,所以我有这个数据结构
class User < ActiveRecord::Base
has_many :companies, :through => :positions
has_many :positions
class Company < ActiveRecord::Base
has_many :positions
has_many :users, :through => :positions
class Position < ActiveRecord::Base
belongs_to :company
belongs_to :user
attr_accessible :company_id, :user_id, :regular_user
end
还有我的数据库结构
create_table "positions", :force => true do |t|
t.integer "company_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "regular_user", :default => true
end
如果我将另一家公司添加到用户公司,则 regular_user 标志始终设置为 true
1.9.3-p125 :013 > @user.companies << Company.last
Company Load (0.3ms) SELECT `companies`.* FROM `companies`
ORDER BY `companies`.`id` DESC LIMIT 1
(0.0ms) BEGIN
SQL (0.2ms) INSERT INTO `positions`
(`company_id`, `created_at`, `regular_user`, `updated_at`, `user_id`)
VALUES
(263, '2012-07-25 13:56:56', 1, '2012-07-25 13:56:56', 757)
有没有办法在插入之前将标志设置为 false
我一直在通过这样做来解决它....这是骇人听闻的
@user.positions.update_all(:regular_user => false) if @user.is_admin?
是否有另一种方法(更清洁)来实现这一目标