How to access params in the callback of a Rails model? - ruby-on-rails

I have a callback of a model that needs to create a dependent object based on another field entered in the form. But params is undefined in the callback method. Is there another way to access it? What's the proper way to pass a callback method parameters from a form?
class User < ActiveRecord::Base
attr_accessible :name
has_many :enrollments
after_create :create_enrollment_log
private
def create_enrollment_log
enrollments.create!(status: 'signed up', started: params[:sign_up_date])
end
end

params are not accessible in models, even if you pass them as a parameter then it would be consider as bad practice and might also be dangerous.
What you can do is to create virtual attribute and use it in your model.
class User < ActiveRecord::Base
attr_accessible :name, :sign_up_date
has_many :enrollments
after_create :create_enrollment_log
private
def create_enrollment_log
enrollments.create!(status: 'signed up', started: sign_up_date)
end
end
Where sign_up_date is your virtual attribute

params will not be available inside the models.
One possible way to do this would be to define a virtual attribute in the user model and use that in the callback
class User < ActiveRecord::Base
attr_accessible :name,:sign_up_date
has_many :enrollments
after_create :create_enrollment_log
private
def create_enrollment_log
enrollments.create!(status: 'signed up', started: sign_up_date)
end
end
you will have to make sure that the name of the field in the form is user[:sign_up_date]

Related

rails_admin how to include a child attribute in create form

I'm trying to make a form to create new record for a model user which has one billing_information. Billing_information has an attribute account_name that I want to include in the form. I tried using the delegate method but it's not working. It produces :-
error: unknown attribute 'billing_information_account_name' for User.
class User < ActiveRecord::Base
accepts_nested_attributes_for :billing_information
has_one :billing_information, inverse_of: :user
delegate :account_name, to: :billing_information, allow_nil: true
rails_admin do
create do
field :name
field :email
field :billing_information_account_name do
def value
bindings[:object].account_name
end
end
end
end
end
Does anyone has a better solution? Thank you.
Sadly, you won't get help from rails admin in this case, but it can be done.
You have to add a new virtual field and handle in a setter the input. Take a look at this example.
class User < ApplicationRecord
has_one :billing_information, inverse_of: :user
# A getter used to populate the field value on rails admin
def billing_information_account_name
billing_information.account_name
end
# A setter that will be called with whatever the user wrote in your field
def billing_information_account_name=(name)
billing_information.update(account_name: name)
end
rails_admin do
configure :billing_information_account_name, :text do
virtual?
end
edit do
field :billing_information_account_name
end
end
end
You can always create the full billing_information using the nested attributes strategy, meaning add the billing_information field and you'll get a nice form to fill all the information.

Rails 4 + Devise: After create user, create XYZ

I have a User model that gets created through Devise, and after it's creation, I would like to automatically create a new Client (another model in my app). The new Client's atrribute, :user_id, should be equal to the :id of the User that was just created. I believe I need to use something like:
class Users::RegistrationsController < Devise::RegistrationsController
after_create :create_client
def create_client
Client.create(:user_id, :id) # Not sure what should go here
end
end
Is this the correct way to accomplish this? Also, if associations are important Client belongs_to :user and User has_one :client
You can add an after_create callback in User model(user.rb), check here for more information on how to create has_one associations.
class User < ActiveRecord::Base
after_save :add_client
def add_client
self.create_client(client_attribute1: value, client_attribute2: value)
end
end

Saving association I.D upon creation?

If I have a user that has_many user_logins and a user_logins that belongs to user - When a user_login is created I'm using UserLogin.create(userlogin_params) and then my strong params permits the user_id column - but this alone is not saving the current users I.D to the column as it is coming out as nil.
How do I make it save the I.D?
User model:
has_many :user_logins
UserLogin model
belongs_to :user
accepts_nested_attributes_for :user
UserLoginController:
...
def create
#user_login = UserLogin.new(user_login_params)
...
end
...
def user_login_params
param.require(:user_login).permit(
:user_login_attribute1,
:user_login_attribute2,
user_attributes: [
:id,
:user_attribute1,
:user_attribute2
]
)
end
Tell me if it helps.
There are 2 issues here at hand.
First: How do you create an association with the parent record automagically there?
Second: How do you do this so your controller action isn't a giant hole waiting for a hacker to stick their nose in.
You need to start from the parent, then build the child, not start with the child and build the parent.
Consider the following:
class User < ActiveRecord::Base
has_many :logins, class_name: "UserLogin"
end
class UserLogin < ActiveRecord::Base
belongs_to :user
end
class UserLoginsController < ApplicationController
def create
if new_user_login(user_login_params).save
redirect_to :wherever
else
render :new
end
end
private
def new_user_login(attrs={})
current_user.logins.create(attrs)
end
def user_login_params
param.require(:user_login).permit(:attr_1, :attr_1)
end
Do not pass IDs into any secure params hash unless that ID is selectable by the user. If you allow an ID into secure params, a hacker can start moving records around to other objects and destroy your database integrity.
If you would like pairing help on this problem live and in person, you can check out my codementor profile at https://codementor.io/rubycasts/#reviews

Ruby on Rails URL: use other models data within to_param method

I'm new to rails. I want to create simple url for the users, I don't want to use friendly_id, if possible. I can already change the url using this
#MyModel.rb
def to_param
address
end
#MyController.rb
#user = Admin::Account.find_by_address(params[:id])
but what I really want is to use name instead of address, but the problem is that, name does not belong to MyModel.rb it balongs to OtherModel.rb, is this even pissible? please help
here is a sample of what I want
#MyModel.rb
def to_param
name #name is a data/column from OtherModel.rb
end
#MyController.rb
#user = Admin::Account.find_by_name(params[:id])
#instead of using find_by_address I can now use find_by_name
here is a sample of my model relations
class Admin::MyModel < ActiveRecord::Base
has_many :other_model, :class_name => 'Admin::OtherModel', dependent: :destroy
end
class Admin::OtherModel < ActiveRecord::Base
belongs_to :my_model, :class_name => 'Admin::MyModel'
end

accessing associations within before_add callback in Rails 3

In Rails 3.2 I have been looking for a way to traverse the associations of an object within the before_add callback.
So basically my use case is:
class User < ActiveRecord::Base
has_and_belongs_to_many :meetings
end
class Meeting < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :comments, :before_add => :set_owner_id
end
class Comment < ActiveRecord::Base
belongs_to :meeting
end
def set_owner_id(child)
child.owner_id = <<<THE USER ID for #user >>>
end
and I am creating a comment within the context of a user:
#user.meetings.first.comments.create
How do I traverse the associations from within the before_add callback to discover the id of #user? I want to set this at model level. I have been looking at proxy_association, but I may be missing something. Any ideas?
You should probably create the comment in the context of the meeting, no? Either way, you should handle this in the controller since you'll have no access to #user in your model.
#comment = Meeting.find(id).comments.create(owner_id: #user, ... )
But if you insist on your way, do this:
#comment = #user.meetings.first.comments.create(owner_id: #user.id)

Resources