How to include multiple model in rails lib - ruby-on-rails

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.

Related

How do I add global Rails helpers like has_many and devise_for?

In Rails, I'd like to apply functionality to many models at the same time, having the ability to add it to other models in the future.
I'm looking for something like...
class Stuff < ActiveRecord::Base
some_tag
end
class Thing < ActiveRecord::Base
some_tag
end
to give those and any other models with
some_tag
the functionality of
class Functionality
has_many :other_things, polymorphic: true
def does_something
end
end
First step: Create a module with the method you want to add to your classes:
module FooFunction
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def does_something(options = {})
...
end
end
end
Next step: Include that module into the class
# in config/initializers/foo_function.rb
class Object
include FooFunction
end

How can I call a class method from Model rails

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

Where to put helper methods needed in multiple models in a Rails app?

As an example:
module ModelHelper
def self.special_function(some_parameter)
do_some_special_thing
end
end
class Student < ActiveRecord::Base
def to_special
ModelHelper.special_function(a_variable_of_here)
end
end
class Teacher < ActiveRecord::Base
def to_special
ModelHelper.special_function(another_variable_of_here)
end
end
Where do I put model_helper.rb?
I generally make a file in lib and include it. Something like lib/special_model.rb:
module SpecialModel
included do
def to_special
do_some_special_thing
end
end
end
Then in app/models/student.rb:
class Student
include SpecialModel
end
You may also want to look at ActiveSupport::Concern for some rails help when working with modules:
http://api.rubyonrails.org/classes/ActiveSupport/Concern.html

Inherit active_record in rails

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

adding class methods to ActiveRecord::Base

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.

Resources