Where does one stick shared model code? I have a method that I want
to share between models. What file should I define that method in if
I want to keep DRY?
Thanks,
-scott
Where does one stick shared model code? I have a method that I want
to share between models. What file should I define that method in if
I want to keep DRY?
Thanks,
-scott
a module in the lib folder would be the right place i think
then you can include it in the models
in lib/my_cool_library.rb
module MyCoolLibrary
def my_cool_method
puts “I do stuff that’s cool”
end
end
Restart server
now, in your models, just do
class User < ActiveRecord::Base
include MyCoolLibrary
end
This will “mix in” this code into your class, making it available to
instance methods.
user = User.new
user.my_cool_method
-Brian
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs