Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module AcceptsDelimtedIdStringFor
# accepts_delimited_id_string_for :careers
#
# becomes
#
# def career_id_strings=(delimited_string)
# self.careers = Career.where(:id => delimited_string.split(",")).all
# end
#
# So...
# object.career_id_strings = ("1,2,3,4")
def accepts_delimited_id_string_for(*associations)
associations.each do |association|
singular = association.to_s.singularize
klass = singular.classify.constantize
define_method :"#{singular}_id_strings=" do |delimited_string|
send(:"#{association}=", klass.where(:id => delimited_string.split(",")).all)
end
end
end
end
ActiveRecord::Base.extend(AcceptsDelimtedIdStringFor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.