Skip to content

Instantly share code, notes, and snippets.

@mikecmpbll
Created April 15, 2015 13:03
Show Gist options
  • Save mikecmpbll/82ecd8264570441de076 to your computer and use it in GitHub Desktop.
Save mikecmpbll/82ecd8264570441de076 to your computer and use it in GitHub Desktop.
hack around adding invalid objects to a has_many association
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">
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