I'm new in Ruby On Rails, so, this is probably going to be a stupid question.
I need to create a table to save the history of the transitions of an attribute. The problem is that transition it's in a diferent class from the one where I'm creating the table.
It goes something like this:
class Family::Parent
class Historic
def add_historic
#code
end
end
class Family::Parent < ActiveRecord::Base
def make_transition
#code
end
end
I know I can use something like ActiveModel::Dirty, but I have no idea how to do it, do you have any idea aout this ?
Thank you!
easiest way is to use audited gem
SEE
https://github.com/collectiveidea/audited
Related
I want that when new entry insert in Logs model column of another model updated after that.
For this I want to write trigger on insertion but I cant find any helping material how to write trigger in rails.
Did you try Callbacks? It seems like this is what you are after.
Maybe something like this:
class Post < ActiveRecord::Base
after_save :add_log_entry
private
def add_log_entry
LogModel.create(message: 'post thingy')
end
end
I am using Ruby on Rails 4.1 and I would like to know if it is good and what are the drawbacks of creating a associated record just after the associator record creation. That is, for example, I have the Article class and the Comment class that belongs_to Article and I would like to create a "default" comment just after an article is created.
Probably I can use a callback method to accomplish that, but are there other approaches to accomplish what I looking for? What should I look at to be "balanced" (eg: class/module dependencies)?
You are looking for something along the lines of...
Class Article
has_many :comments
after_create :create_first_comment!
def create_first_comment!
comments.create
end
end
Quite new to Rails and have run into an issue I just can't seem to figure out.
I have 2 models, User & Post. Users will have a "name" attribute, Posts will have a "title" attribute.
In both cases, I would like to also maintain a slug that will, on before_save, convert the appropriate column to a "sluggified" version and store that as the slug. I've already got the logic I want in place and have had this working, however, I'd like to abstract the behavior into a Concern.
I cannot seem to figure out a way to set this up - mostly because of the dynamic nature of the source field. I'd like to be able to do something like the following:
class User < ActiveRecord::Base
include Sluggable
act_as_slug :name
end
class Post < ActiveRecord::Base
include Sluggable
act_as_slug :title
end
Unfortunately, no matter what I've tried on the implementation of the concern, I've run into walls.
While I'd like to know what type of implementation is possible either way, I'd also be interested in hearing if this is a good use case for concerns or not?
This seems to work, in the event anyone else is looking for an answer (definitely open to better suggestions from those with more experience). The models look as suggested in the original post.
module Sluggable
extend ActiveSupport::Concern
included do
before_save :generate_slug
class_attribute :sluggable_attribute
def generate_slug
self.sluggify(self.class.sluggable_attribute)
end
def sluggify(attribute)
# Sluggify logic goes here
end
end
module ClassMethods
def acts_as_slug(value)
self.sluggable_attribute = value
end
end
end
Basically I would like to add the ability to vote on tags, so I would like to have a priority column for each different model's tag.
Any ideas about how to do this?
I suspect I need to make a new migration, but I don't know what to make it for. What would the migration be?
Thanks!
As I remember, acts_as_taggable creates a table called tags, so you add a field to that table:
rails g migration add_votes_to_tag votes:integer
and add your logic to vote on tag.
P.S. Not sure if I understood correctly your question.
If you want to extend the regular usage of the tag class, seems to be the case, and create a special case for those tags that needs to be counted you can rely on a hook method from the core named [find_or_create_tags_from_list_with_context][1]
class Company < ActiveRecord::Base
acts_as_taggable_on :markets, :locations
def find_or_create_tags_from_list_with_context(tag_list, context)
if context.to_sym == :markets
MarketTag.find_or_create_all_with_like_by_name(tag_list)
else
super
end
end
end
I have a multi-tenant rails app up and running.
Models that i want scoped to the current tenant (like this article model here) inherit the tenantscoped model like this
class Article < TenantScoped
end
this works great. i only recieve objects scoped to the current tenant.
but now im creating an admin interface where i want to be able to add articles to all tenants. but my admin interface is acting as a tenant and the models are being scoped to it.
Which ends with no entries being shown.
I am proposing that the best solution to this is to conditionally inherit from the tenant scoped model like this
class Article
unless SudoTenant.current?
< TenantScoped
else
< ActiveRecord::Base
end
end
i've been searching around to conditional inheritance for ruby classes and havent found anything yet. my syntax is wrong here or is this even possible?
Thanks in advance
You can define the class using the block syntax:
if SudoTenant.current?
Article = Class.new(ActiveRecord::Base) do
# your code
end
else
Article = Class.new(TenantScoped) do
# your code
end
end
I strongly recommend to use mixins instead of conditionally inheriting, it's cleaner, clearer and more obvious.
Not exactly what you're asking, but I happen to be doing the same thing (global articles on a tenant app), and I just created a Tenant for Admin for using it in my global Articles.
I've got something like this:
#article.rb
def self.global
unscoped.where(:company => Company.admin)
end
#company.rb
def self.admin
where(:name => 'admin').first # this can pretty much be anything that fits to you.
end