app/services/mail_service
class MailService
def subscribe
code
end
end
User Model
class User < ActiveRecord::Base
after_create :user_subscribe
def user_subscribe
Services::MailService.new
end
end
It shows error like uninitialized constant Services
You dont have to write Services just write :-
MailService.new
Try this
require 'MailService.rb'
class User < ActiveRecord::Base
after_create :user_subscribe
def user_subscribe
MailService.new
end
end
Related
In PilotRaceLap model class I am trying to call a method from another class but keep getting NoMethodError.
class PilotRaceLap < ActiveRecord::Base
acts_as_paranoid
belongs_to :race_session
def mark_splitted
prl = RaceSessionAdapter.track_lap_time(pilot.transponder_token,lap_time/2) <<<ERROR HERE
end
track_lap_time is declared in this adapter class:
class RaceSessionAdapter
attr_accessor :race_session
def track_lap_time(transponder_token,delta_time_in_ms)
...
end
race_session model class starts like that:
class RaceSession < ActiveRecord::Base
acts_as_paranoid
has_many :pilot_race_laps
has_many :race_attendees
track_lap_time should be a class method (it's instance method right now). Just add self. before name.
def self.track_lap_time()
...
end
or write this method in class << self block
class << self
def track_lap_time()
...
end
end
Use RaceSessionAdapter.new.track_lap_time()
In my /lib i have this class below:
module Application
class Post < ActiveRecord::Base
attr_accessor :id
def artilce_content
post.articles.content
end
private
def post
Post.find(id)
end
end
end
But the problem is article is undefined.
NoMethodError: undefined method ziptag_type' for #<Application::Api::V2::Ziptag:0x00000008edc120>
The question is, how can I use or include multiple models in /lib? I tried adding class User < ActiveRecord::Base; end above module
class Article < ActiveRecord::Base; end
module Application
class Post < ActiveRecord::Base; end
:
:
:
end
end
but is doesn't worked.
The best way here is to use require.
require 'your_model'
In your case, it should be:
require 'article'
module Application
class Post < ActiveRecord::Base
attr_accessor :id
def artilce_content
post.articles.content
end
private
def post
Post.find(id)
end
end
end
I've got the answer. It should be:
class Article < ActiveRecord::Base; attr_accessor :column1, :other_column end
Not:
class Article < ActiveRecord::Base; end
Now, it's working fine on me.
I want to know how i can use create callback like before_create or after_create for the two dependent models.
class User < ActiveRecord::Base
end
class Member < ActiveRecord::Base
end
Lets suppose i have two models called User and Member and i want to create a member whenever any user will be created and want to create user whenever any member will be created .
If i will use the after_create or before_create callback in both the models it will run as never ending loop .so how this can be done.
Just check if either of the association exists in db before creating it in after_create callback, something like this:
class User < ActiveRecord::Base
after_create :create_member
private
def create_member
unless self.member?
# create member
end
end
end
class Member < ActiveRecord::Base
after_create :create_user
private
def create_user
unless self.user?
# create user
end
end
end
Right now my classes are look like this.
class BalanceName < ActiveRecord
def before_validation
set_blank_attributes_to_nil(#attributes)
end
end
class Balance < ActiveRecord
def before_validation
set_blank_attributes_to_nil(#attributes)
end
end
I want to inherite activer record into one class and than want to inherite that class into other classes like.
I want something like this.
class CommonActiveRecord < ActiveRecord::Base
def before_validation
set_blank_attributes_to_nil(#attributes)
end
end
class BalanceName < CommonActiveRecord
def before_validation
super
end
end
class Balance < CommonActiveRecord
def before_validation
super
end
end
You can do exactly as you have done except you do not need to redefine the before_validation methods in your subclasses (though I guess these may be here prior to being filled with more specific validation).
You will also need to indicate to rails that your CommonActiveRecord class is abstract and therefore is not persisted by adding:
class CommonActiveRecord < ActiveRecord::Base
self.abstract_class = true
end
You can create module (e.g. lib/common_active_record.rb):
module CommonActiveRecord
def before_validation
set_blank_attributes_to_nil(#attributes)
end
end
And then in your model simply include it:
class BalanceName < ActiveRecord::Base
include CommonActiveRecord
end
class Balance < ActiveRecord::Base
include CommonActiveRecord
end
I have created an instance method which is also a callback (if that makes sense) which does some stuff thats irrelevant. I would love to be able to just call:
class Model < ActiveRecord::Base
fix_camelcase_columns
end
Instead at the moment I have this:
def after_find
self.class.columns.each do |column|
self.instance_eval("def #{column.name.to_underscore}; self.#{column.name}; end;")
end
end
I would love to abstract this and use it on other classes. Any pointers?
Well, you can open up ActiveRecord::Base and throw a method there:
class ActiveRecord::Base
def self.fix_camelcase_columns
define_method :after_find do
...
end
end
end
For a cleaner way, create a module:
module CamelcaseFixer
def self.included(base)
base.extend(self)
end
def fix_camelcase_columns
define_method :after_find do
...
end
end
end
and then in your model do
class Model < ActiveRecord::Base
include CamelcaseFixer
fix_camelcase_columns
end
Didn't test the code, see if it works.