Rails populate with default entries after database table was created - ruby-on-rails

I am having a checklist table with following columns:-
id | product_id | content | archived
Once the user signs up product table is created and corresponding checklist table is also created.
I want to add default 5 entries with data for checklist table for each user after he/she signs up. Any help?

I would solve this with a service object.
# app/services/default_checklist_service.rb
class DefaultChecklistService
attr_accessor :user, :defaults
def initialize(user, defaults = nil)
#user = user
#defaults = defaults || self.class.load_defaults
end
def self.call(user, defaults = nil)
new(user, defaults).call
end
def call
self.defaults.map do |attributes|
user.checklists.create(attributes)
end
end
private
def self.load_defaults
YAML.load_file(Rails.root.join('config', 'default_checklist.yml'))
.try(:[], 'checklists')
end
end
This creates a single purpose object which is easily tested. VS model callbacks which add more responsibilities to an already god like model and which are tricky to test and control when and where they fire.
# config/default_checklists.yml
checklists:
-
foo: 1
bar: 2
-
foo: 2
bar: 3
Note that this will create n (where n is the number of default items) separate insert queries which is not very fast. If the performance becomes an issue than you can use a mass insert instead:
# app/services/default_checklist_service.rb
class DefaultChecklistService
# ...
def call
sql = "INSERT INTO users(user_id, foo, bar) VALUES "
values = defaults.map do |a|
"(#{#user.id}, #{a["foo"]}, #{a["bar"]})"
end
ActiveRecord::Base.connection.execute( sql + values.join(', ') )
end
end
Since creating the defaults is expensive and will slow your tests down I would not use a model callback. Instead call the service from the controller where you actually want the seeding to happen:
class UsersController
def create
#user = User.create(user_params)
if #user.save
DefaultChecklistService.call(#user)
# ...
else
# ...
end
end
end

Use after create callback:
User < ActiveRecord::Base
has_many :check_lists
after_create :add_default_checklist
def add_default_checklist
default_check_lists.each do |check_list_data| # Define way to get default values
check_lists.create(check_list_data)
end
end
end
You'll need to add user_id column to checklist

Related

How to pass dynamic params in Rails?

I want some of my model attributes to predefined dynamically. I have various models.And now I want My Bill model to create objects using other model instances.
Models :
leave.rb # belongs_to :residents
resident.rb # has_many:leaves,has_many:bills,has_one:account
bill.rb # belongs_to:residents
rate_card.rb # belongs_to:hostel
account.rb # belongs_to:resident
hostel.rb
now here is my bills controller create method :
def create
#bill = Resident.all.each { |resident| resident.bills.create(?) }
if #bill.save
flash[:success]="Bills successfully generated"
else
flash[:danger]="Something went wrong please try again !"
end
end
I want to build bill using all of the models eg:
resident.bills.create(is_date:using form,to_date:using form,expiry_date:using form,amount:30*(resident.rate_card.diet)+resident.rate_card.charge1+resident.rate_card.charge2)+(resident.account.leaves)*10+resident.account.fine)
///////Is this possible ?
And how to use strong params here ?
Pls help me out thxx..
I think the Rails way for this logic you want is with callbacks if you want calculated attributes either on create, update or delete, meaning attributes that depend on other models. For instance:
class Bill < ActiveRecord::Base
...
before_create :set_amount
...
protected
def set_amount
self.amount = 30 * self.resident.rate_card.diet + self.resident.rate_card.charge1 + self.resident.rate_card.charge2 + (self.resident.account.leaves) * 10 + self.resident.account.fine
end
end
If you want this logic to be used when updating the record also, then you should use before_save instead of before_create.
After you do this, you should accept the usual params (strong) of Bill model, as in:
def bill_params
params.require(:bill).permit(:is_date, :to_date, :expiry_date)
end
So your create call would be like:
resident.bills.create(bill_params)
Also, be wary of your create action, you should probably create a method either on your Bill or your Resident model that uses transactions to create all bills at the same time because you probably want either every bill created or none. This way you won't have the Resident.all.each logic in your BillsController.
create takes a hash, you can:
create_params = { amount: 30*(resident.rate_card.diet) }
create_params[:some_field] = params[:some_field]
# and so on
resident.bills.create(create_params)
or:
obj = resident.bills.build(your_strong_parameters_as_usual)
obj.amount = # that calculation
obj.save!
I'm confused at your syntax of your controller. #bill is being set to the value of a loop, which feels off. Each loops return the enumerable you cycle through, so you'll end up with #bill = Resident.all with some bills being created on the side.
What your controller really wants to know is, did my many new bills save correctly?
This seems like a perfect place to use a ruby object (or, colloquially, a Plain Old Ruby Object, as opposed to an ActiveRecord object) to encapsulate the specifics of this bill-generator.
If I'm reading this right, it appears that you are generating many bills at once, based on form-inputted data like:
is_date
to_date
expiry_date
...as well as some data about each individual resident.
Here's the model I'd create:
app/models/bill_generator.rb
class BillGenerator
include ActiveModel::Model
# This lets you do validations
attr_accessor :is_date, :to_date, :expiry_date
# This lets your form builder see these attributes when you go form.input
attr_accessor :bills
# ...for the bills we'll be generating in a sec
validates_presence_of :is_date, :to_date, :expiry_date
# You can do other validations here. Just an example.
validate :bills_are_valid?
def initialize(attributes = {})
super # This calls the Active Model initializer
build_new_bills # Called as soon as you do BillGenerator.new
end
def build_new_bills
#bills = []
Resident.all.each do |r|
#bills << r.bills.build(
# Your logic goes here. Not sure what goes into a bill-building...
# Note that I'm building (which means not-yet-saved), not creating
)
end
def save
if valid?
#bills.each { |b| b.save }
true
else
false
end
end
private
def bills_are_valid?
bill_validity = true
#bills.each do |b|
bill_validity = false unless b.valid?
end
bill_validity
end
end
Why all this mess? Because in your controller you can do...
app/controllers/bill_controller.rb
def create
#bill_generator = BillGenerator.new(bill_generator_params)
if #bill_generator.save?
# Redirect to somewhere with a flash?
else
# Re-render the form with a flash?
end
end
def bill_generator_params
params.require(:bill_generator).permit(:is_date, :to_date, :expiry_date)
# No extra garbage. No insecurity by letting all kinds of crud through!
end
...like a BillGenerator is any old object. Did it save? Great. It didn't, show the form again.
Now, my BillGenerator won't just be copy-and-paste. Your 'build_new_bills' probably will have some of that math you alluded to, which I'll leave to you.
Let me know what you think!
you can do it by using params.permit! as this allows any parameters to be passed. here's an example:
def create
...
#bill = Resident.all.each { |resident| resident.bills.create(any_params) }
end
private
def any_params
params.permit!
end
be careful with this of course, as you are opening this up to potential exploits.

Recommended practice for passing current user to model

Given a model Orderstatus with attributes private_status:string, and private_status_history:json(I'm using Postgresql's json). I would like to record each status transition, together with the user who made the change.
Ideally it would be something like:
class Orderstatus < ActiveRecord::Base
after_save :track_changes
def track_changes
changes = self.changes
if self.private_status_changed?
self.private_status_history_will_change!
self.private_status_history.append({
type: changes[:private_status],
user: current_user.id
})
end
end
end
class OrderstatusController <ApplicationController
def update
if #status.update_attributes(white_params)
# Good response
else
# Bad response
end
end
end
#Desired behaviour (process not run with console)
status = Orderstatus.new(private_status:'one')
status.private_status #=> 'one'
status.private_status_history #=> []
status.update_attributes({:private_status=>'two'}) #=>true
status.private_status #=> 'two'
status.private_status_history #=> [{type:['one','two'],user:32]
What would be the recommended practice to achieve this? Apart from the usual one using Thread. Or maybe, any suggestion to refactor the structure of the app?
So, I finally settled for this option ( I hope it's not alarming to anyone :S)
class Orderstatus < ActiveRecord::Base
after_save :track_changes
attr_accessor :modifying_user
def track_changes
changes = self.changes
if self.private_status_changed?
newchange = {type:changes[:private_status],user: modifying_user.id}
self.update_column(:private_status_history,
self.private_status_history.append(newchange))
end
end
end
class OrderstatusController <ApplicationController
def update
#status.modifying_user = current_user # <---- HERE!
if #status.update_attributes(white_params)
# Good response
else
# Bad response
end
end
end
Notes:
- I pass the from the Controller to the Model through an instance attribute modifying_user of the class Orderstatus. That attribute is ofc not saved to the db.
- Change of method to append new changes to the history field. I.e. attr_will_change! + save to update_column + append

Ruby on Rails: how do I create an initializer method in the model or should I set all the default params in a controller?

I have a field in my model for date created, this is not passed from the form and is currently set in the create method of my controller.
Should this be in my model instead in some sort of initializer method? If so what would that method look like?
I have other fields I want to set as a default each a record is created so I'm trying to find out where is the accepted standard place to put these. I'm starting to think it should be the model as if the model was ever called outside the controller it wouldn't have all this logic.
I generally create builders and never use directly the standard Rails method create.
The point is to gather all the logic in one place with particular cases etc...
Basically in controllers I end up calling the builders this way:
#my_model_instance = MyModelBuilder.new(current_user, params[:my_model]).build
#my_model_instance = MyModelBuilder.new(current_user, params[:my_model]).create
All my builders live in /app/builders
Here is a very basic example:
class MyModelBuilder
attr_accessor :params, :user, :my_model
# consider using a Struct if you keep a very basic initializer
def initialize(user, params)
self.user = user
self.params = params
end
def build
my_model
end
def create
my_model.tap{|m| m.save }
end
def my_model
#my_model ||= MyModel.new(default_values.merge(params))
end
def default_values
{
foo: 'bar'
}
end
end
Rails already manages the date of creation and update of your records.
If your model has a created_at field or an updated_at field they will be filled with the time of creation and update of your model.
You can generate those fields easily in a migration, for instance :
create_table :hello do
t.timestamps
end
Now, for default values, you can fill them in the initialize method of the model :
def initialize(*args)
self.field = default_value
super(*args)
end

Create new records on before_save

While creating new records. I need to create more records for the same model.
Example ::
class XYZ < ActiveRecord
def before_save
# At this point object is already initialized ..
# And it's containing values.
# At this point i want to create 10 more records for the same class.
# something like this
XYZ.new(:att1 => value1,:att2 => value2,:att3 => self.att1)
end
end
How may i handle this type of scenario ?
On which call back i have to create more records for the same model ?
First, this sounds like bad engineering, try to rethink your model in a way that makes what you need.
maybe if you need to create 10 models of something, do not use the activerecord hooks, otherwise you might incur in infine loops.
I would recommend
class XYZ < ActiveRecord
def self.create10(original_xyz)
10.times do
clone = original_xyz.clone
clone.save
end
end
end
and where in your controller or wherever you have the need to create 10 more, call:
new_xyz = XYZ.new(:att1 => value1,:att2 => value2,:att3 => self.att1)
new_xyz.save
XYZ.create10(new_xyz)
but if you really need to create 10 more on a hook (like before save), do:
class XYZ < ActiveRecord
before_save create10
attr_acessor :cloned
def create10
return if cloned # this will prevent infinit loooooooooooooooop
10.times do
clone = self.clone
clone.cloned = true
clone.save
end
end
end
I did not run this, so, try it first.
class XYZ < ActiveRecord
def after_initialize
# At this point object is already initialized ..
# And it's containing values.
# At this point i want to create 10 moew records for the same class.
# something like this
#XYZ.new(:att1 => value1,:att2 => value2,:att3 => self.att1)
x = 10 #an integer
x.times do |task|
Model.create(:key => :value)
end
end
end

Preference values - static without tables using a model with virtual attributes

Im trying to eliminate two tables from my database. The tables are message_sort_options and per_page_options. These tables basically just have 5 records which are options a user can set as their preference in a preferences table. The preferences table has columns like sort_preferences and per_page_preference which both point to a record in the other two tables containing the options. How can i set up the models with virtual attributes and fixed values for the options - eliminating table lookups every time the preferences are looked up?
Create a app_config.yml file in config directory.
page:
small: 10
medium: 20
large: 30
sort:
name: name DESC
amount: amount ASC
date: created_at DESC
Create UserOptions class in models directory.
class UserOptions
def self.page_option key
options['page'][key] rescue nil
end
def self.sort_option key
options['sort'][key] rescue nil
end
def self.options
#options ||= YAML.load_file( File.join(RAILS_ROOT,
"config", "app_config.yml")) rescue {}
end
# use this in the view to set the preference
def self.page_collection
option_collection 'page'
end
# use this in the view to set the preference
def self.sort_collection
option_collection 'sort'
end
def self.option_collection key
(options[key]|| {}).to_a
end
end
Configure your models:
class User
has_one :preference
end
class Preference
def sort_preference(default = nil)
UserOptions.sort_option(attributes['sort_preference']) || default
end
def per_page_preference(default = nil)
UserOptions.page_option(attributes['per_page_preference']) || default
end
end
Now you can do the following:
current_user.preference.per_page_preference
# use 10 as the page size if no value is given
current_user.preference.per_page_preference(10)
Try this:
class MessageSortOption
def self.get_cached_option(id)
# store the hash of the options in a class variable
(##option_cache ||= Hash[ *all.collect{|o| [o.id, o]}.flatten])[id]
end
end
class PerPageOption
def self.get_cached_option(id)
# store the hash of the options in a class variable
(##option_cache ||= Hash[ *all.collect{|o| [o.id, o]}.flatten])[id]
end
end
class User
has_one :preference
end
class Preference
def sort_preference
MessageSortOption.get_cached_option(attributes['sort_preference'])
end
def per_page_preference
PerPageOption.get_cached_option(attributes['per_page_preference'])
end
end
Now you can access the preference as follows:
current_user.preference.sort_preference
current_user.preference.per_page_preference

Resources