Created
April 15, 2015 13:03
-
-
Save mikecmpbll/82ecd8264570441de076 to your computer and use it in GitHub Desktop.
hack around adding invalid objects to a has_many association
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
u = User.new | |
u.valid? == false | |
u.save(validate: false) | |
params = { dont_validate_user_ids: true, user_ids: [1] } | |
UserGroup.create(params) | |
User.first | |
=> #<User id: 1, name: nil, user_group_id: 1, created_at: "2015-04-15 12:37:58", updated_at: "2015-04-15 12:37:58"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserGroup < AR::Base | |
attr_accessor :dont_validate_user_ids | |
has_many :users | |
def user_ids=(user_ids) | |
if dont_validate_user_ids | |
User.where(id: user_ids).update_all(user_group_id: id) | |
else | |
super | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment