With the following code I would like to create a database user. For this I want to use the Active Record Transactions. Sadly my code doesn't work, so is somebody able to help me?
begin
ActiveRecord::Base.transaction("CREATE USER " + #user.name + " IDENTIFIED BY "1234")
rescue => e
puts e.to_s
end
I had a chance to think about this, basically you need to change the database to the system one.
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:username => "me",
:password => "secret",
:database => "activerecord"
)
:database will be the name of the database that contains the User table.
Then you can wrap the User creation in a transaction, you would do it like this:
ActiveRecord::Base.transaction do
User.create({:name => "my_name"})
end
The attributes used should match what you find in the system User table.
There are probably things you should do like closing connections and such before you do this, but that depends on where you are calling this from.
Related
I have mailer service where users can upload an .xls file with emails and some other user related data to send an email campaign.
I was having some timeout issues since it takes some seconds to process (as I do some validation and configuration for each email to be sent, eg: save records to database, check if an email was sent in the last 30 days, create personalised html code for each email (to create links that contain the email address as a parameter, etc).
After some research, moving this to a delayed job seemed reasonable, as suggested in this rails cast. The only problem is that I am having an error that says uninitialized constant Mandrill::API, it seems that when I run the job, the call require 'mandrill' doesn't work.
I created the task in my model file. Something like this
class Mailer < ActiveRecord::Base
attr_accessible :email, :lastname, :name
def self.send_mail(emails)
[...a lot of code here...]
require 'mandrill'
m = Mandrill::API.new ENV['MANDRILL_APIKEY']
message = {
:subject=> template.subject,
:from_name=> template.from_name,
:from_email=> from + "#" + email_domain,
:to=>mails,
:global_merge_vars=> [
{ :name => 'GREETING', :content => template.greeting},
{ :name => 'CONT1', :content => template.message},
{ :name => 'IMAGE', :content => image_url},
],
:html=>email_template,
:preserve_recipients => false,
:merge_vars => email_contents,
}
sending = m.messages.send message
end
end
from my controller I call Mailer.send_mails(emails) and it works just fine, but if I call Mailer.delay.send_mails(emails) I get the error. How can I fix this?
I have tried adding require 'mandrill' at the beginning of the class, before and after class Mailer < ActiveRecord::Base but nothing seems to work
Make sure to restart the delayed_job daemon to pick up any code changes. It does not auto-reload like the Rails development environment does.
I have setup devise on a site and now the client just wants to be able to use 1 master password and not allow anyone else to create accounts. I removed the create account links from the login page and I also extended my RegistrationsController to look like this.
class CustomRegistrationsController < Devise::RegistrationsController
# used to override normal create behavior
def create
redirect_to user_session_path
end
# used to override normal new behavior. Redirects user back to the login page
def new
redirect_to user_session_path
end
end
Then in my routes file I have
devise_for :users, :path => '', :path_names => { :sign_in => 'login', :sign_out => 'logout'}, :controllers => { :registrations => "custom_registrations"}
Now I'm trying to find a way where I can either hardcode a master username and password or come up with a better approach to this.
This is by no means an elegant solution, but you could simply use a migration (or the database seed file, if the database is still new enough to be generated that way) to create a single User object with the desired login info, and add a validation on User that returns false if there's already an existing User.
In my app, I use devise to generate User model, add some attributes like job, role,... to this.
In rails_admin page, when edit an user, I only show fields: email, job, role, idcard (no include password and password_confirmation fields)
When I click Save, it show error because the password is shorter than 6 characters.
In the console, if I try
User.last.update_attributes(:role => "admin", "idcard" => "1233131")
It'll get the same error!
How can I fix this? Because the rails_admin (I think and be sure) didn't use update in registrations_controller to update user!
oh, yeah! I just fix my own problem by delete two validate of user model
validates :password, :length => { :minimum => 6 }
validates :password, :confirmation => true
and when I type
User.first.update_attributes(:email => "abc#yahoo.com")
It worked!!!
In update user form, I want to split password from rest of the form.
In user model I validate it.
I want to have
form 1
User infos fields
form 2
password and confirm fields.
in php I would do something like if post password is empty, ignore it.
is there something like
if :password
do validation
end
I need the model part, I have the rest.
just need an idea how to split validation in model or how to achieve this.
I hope i did understand you
class User << AR
validates :password,
:lenght => {:between => 3..20},
:confirmation => true,
:if => Proc.new { self.password }
end
Does anyone know how to handle the session object manually?
I know devise stores it in the database, but there must be a way to set the session manually.
Try warden.set_user(resource, :scope => scope)
This is an example to test that a user can only see contracts to which he has access. (has_role! and has_no_role! is from acl9 - great gem to manage access control)
describe "GET index (logged in)" do
it "#contracts contains only contracts on which user has admin role" do
coA = Factory.create(:contract,:contract_name => "contract_A" )
coB = Factory.create(:contract,:contract_name => "contract_B" )
userA = Factory.create(:user, :username => "userA")
userA.has_role! :admin, coA
userA.has_no_role! coB
warden.set_user(userA, :scope => "user")
get :index, :locale => "fr"
assigns(:contracts).should eq([coA])
end
end