问题
Can someone help me figure out what code I should put in my change_default method?
I would like to be able to do something like this...
user = User.find(1)
user.companies.first.default!
or
user.companies.find_by_company_id(2).default!
My code:
class Role < ActiveRecord::Base
before_save :change_default
belongs_to :user
belongs_to :company
def change_default
#update default field for to false for current scope
#Update current record default field to true
#this is what I currently have, but it's not setting my defaults to false
if self.default == true
roles = Roles.where( :user_id => self.user_id)
roles.update_all(:default => false)
end
end
def default!
self.default=true
self.save
end
end
class User < ActiveRecord::Base
has_many :roles
has_many :companies, :through => :roles
end
class Company < ActiveRecord::Base
has_many :roles
has_many :users, :through => :roles
end
Code run:
pry(main)> a.roles.last.default!
(0.1ms) begin transaction
SCOPING
SQL (0.4ms) UPDATE "roles" SET "default" = 'f' WHERE "roles"."user_id" = 1
(2.0ms) commit transaction
=> true
[5] pry(main)> a.roles
=> [#<Role id: 1, company_id: 1, name: nil, view_document: nil, edit_document: nil, upload_document: nil, delete_document: nil, review_document: nil, company_info: nil, company_clients: nil, company_users: nil, company_admin: nil, created_at: "2014-08-04 20:10:23", updated_at: "2014-08-04 22:29:40", user_id: 1, default: true>,
#<Role id: 2, company_id: 2, name: nil, view_document: nil, edit_document: nil, upload_document: nil, delete_document: nil, review_document: nil, company_info: nil, company_clients: nil, company_users: nil, company_admin: nil, created_at: "2014-08-04 20:11:10", updated_at: "2014-08-04 20:11:10", user_id: 1, default: nil>,
#<Role id: 3, company_id: 3, name: nil, view_document: nil, edit_document: nil, upload_document: nil, delete_document: nil, review_document: nil, company_info: nil, company_clients: nil, company_users: nil, company_admin: nil, created_at: "2014-08-04 20:11:14", updated_at: "2014-08-04 22:29:16", user_id: 1, default: true>]
As you can see the update ran, but the default values are not set to false. If you have any recommendations or things I can check, please let me know.
回答1:
Needed to exclude the current record that is being modified. Added the where.not clause
def change_default
if self.default_changed? && self.default == true
roles = Role.where( :user_id => self.user_id)
roles = roles.where.not(:id => self.id)
roles.update_all(:default => false)
end
end
回答2:
Looks like in the example you never call save
on the roles. Therefore, your before_save :change_default
is not being executed. Perhaps adding role.save
in def default!
would at least have the before_save
be executed because I'm not sure that your current console test is actually running the change_default
method.
If I am wrong and it actually is running, then I'm not sure why your block would not set the default
value to false.
来源:https://stackoverflow.com/questions/25127986/before-save-change-default-value-for-many-to-many