rails_admin collection action executed twice - ruby-on-rails-3

I'm using rails_admin to email all users when a button is clicked. The action is called twice instead of once.
lib/rails_admin_email_everyone.rb:
require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'
module RailsAdmin
module Config
module Actions
class EmailEveryone < RailsAdmin::Config::Actions::Base
register_instance_option :visible? do
authorized?
end
register_instance_option :link_icon do
'icon-envelope'
end
register_instance_option :collection? do
true
end
register_instance_option :member? do
false
end
register_instance_option :controller do
Proc.new do
#topics_with_owners = Topic.where('user_id IS NOT NULL')
#topics_with_owners.each do |topic|
#reminder = Reminder.new(:topic_id=> topic.id,
:to_user_id => student.id, :from_user_id => current_user.id)
if #reminder.save
TopicMailer.reminder(#reminder.topic).deliver
#reminder.update_attribute(:sent, true)
else
logger.error "Error sending email reminder: "
+ #reminder.errors.full_messages.map {|msg| msg}.join(',')
end
end
flash[:success] = "Reminders successfully sent."
redirect_to back_or_index
end
end
end
end
end
end
config/initializers/rails_admin.rb:
require Rails.root.join('lib', 'rails_admin_email_everyone.rb')
RailsAdmin.config do |config|
config.authenticate_with do
redirect_to(main_app.root_path, flash:
{warning: "You must be signed-in as an administrator to access that page"})
unless signed_in? && current_user.admin?
end
module RailsAdmin
module Config
module Actions
class EmailEveryone < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
end
end
end
end
config.actions do
# root actions
dashboard # mandator y
# collection actions
index # mandatory
new
export
history_index
bulk_delete
# member actions
show
edit
delete
history_show
show_in_app
email_everyone do
visible do
bindings[:abstract_model].model.to_s == "Reminder"
end
end
end
end
When I examine my log I see it executes twice, but with slightly different parameters. One including pjax. I found this stackoverflow question relating a similar issue to the pjax timeout:
Started GET "/admin/reminder/email_everyone?_pjax=%5Bdata-pjax-container%5D" for 127.0.0.1 at 2012-12-01 20:28:12 -0900
Processing by RailsAdmin::MainController#email_everyone as HTML
Parameters: {"_pjax"=>"[data-pjax-container]", "model_name"=>"reminder"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Topic Load (1.3ms) SELECT "topics".* FROM "topics" WHERE (user_id IS NOT NULL)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.2ms) BEGIN
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Topic Load (0.5ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = 4398 LIMIT 1
SQL (6.5ms) INSERT INTO "reminders" ("created_at", "from_user_id", "sent", "topic_id", "to_user_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Sun, 02 Dec 2012 05:28:12 UTC +00:00], ["from_user_id", 1], ["sent", false], ["topic_id", 4398], ["to_user_id", 1], ["updated_at", Sun, 02 Dec 2012 05:28:12 UTC +00:00]]
(1.1ms) COMMIT
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Rendered topic_mailer/reminder.text.erb (375.8ms)
Started GET "/admin/reminder/email_everyone" for 127.0.0.1 at 2012-12-01 20:28:14 -0900
Processing by RailsAdmin::MainController#email_everyone as HTML
Parameters: {"model_name"=>"reminder"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Topic Load (1.1ms) SELECT "topics".* FROM "topics" WHERE (user_id IS NOT NULL)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.2ms) BEGIN
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Topic Load (0.5ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = 4398 LIMIT 1
SQL (7.1ms) INSERT INTO "reminders" ("created_at", "from_user_id", "sent", "topic_id", "to_user_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Sun, 02 Dec 2012 05:28:14 UTC +00:00], ["from_user_id", 1], ["sent", false], ["topic_id", 4398], ["to_user_id", 1], ["updated_at", Sun, 02 Dec 2012 05:28:14 UTC +00:00]]
(1.0ms) COMMIT
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Rendered topic_mailer/reminder.text.erb (368.9ms)

I found the same issue,
I just added following code into lib/rails_admin_custom_action.rb
i.e Added pjax? option to false
module RailsAdmin
module Config
module Actions
class EmailEveryone < RailsAdmin::Config::Actions::Base
register_instance_option :pjax? do
false
end
end
end
end
end
may this will help you..

There's a pull request marked as good to merge that creates a pjax option for rails_admin. Unfortunately, it requires tests and there hasn't been any action for 3 months:
https://github.com/sferik/rails_admin/pull/1295

Related

Check if a record is present in a has-many association

I have a rails-api application in which users can follow other users.
To check if an user already follows another user, I need to include a query in the attributes and because of that, I have always a N+1 query problem.
Here is my code:
Index action in user controller:
def index
#users = ::User.all.paginate(page: params[:page])
end
The followers will always be included by a default_scope in the User model.
index.json.jbuilder:
json.partial! 'attributes', collection: #users, as: :user
_attributes.json.jbuilder:
json.extract! user, :id, :firstname, :lastname, :username, :follower_count
is_follower = user.follower.find_by(id: current_user.id).present? if current_user
json.following is_follower
And as a result:
User Load (0.1ms) SELECT "users".* FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = $1 [["followed_id", 14]]
Rendered v1/user/users/_attributes.json.jbuilder (1.3ms)
User Load (0.1ms) SELECT "users".* FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = $1 [["followed_id", 9]]
Rendered v1/user/users/_attributes.json.jbuilder (1.4ms)
User Load (0.1ms) SELECT "users".* FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = $1 [["followed_id", 13]]
Is there some workaround or is it somehow possible to generate a dynamic attribute in the SQL query which includes the boolean value if the user follows the other user?
Thank you very much in advance.
My first thought would be to eager load the followers using the .includes method when you get the list of users like this #users = ::User.all.includes(:followers).paginate(page: params[:page]). But perhaps, I'm not understanding your question correctly? Let me know if that works or if I should focus my answer on a different subject. Thanks!
EDIT: Correct answer from the comments below:
Perhaps you can try user.followers.include?(current_user) to make use of the pre-loaded followers association.

How to fix a multiple assignment issue? on Rails 4

So, I have 3 tables Accounts, Users, and Subscriptions all of which have the deleted_at column. Now when I delete an account the request gets sent to all models at once causing a Multiple Assignments error as shown below:
Started DELETE "/admin/accounts/3" for 127.0.0.1 at 2014-10-27 19:38:48 +0200
Processing by SaasAdmin::AccountsController#destroy as HTML
Parameters: {"authenticity_token"=>"qo8NxYhezJUz2YylGxHlx2ou125UNoRuEcXbrnzyiN4=", "id"=>"3"}
AppSetting Load (0.3ms) SELECT "app_settings".* FROM "app_settings" ORDER BY "app_settings"."id" ASC LIMIT 1
Tag Load (0.2ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT 1 [["id", 22]]
SaasAdmin Load (0.2ms) SELECT "saas_admins".* FROM "saas_admins" WHERE "saas_admins"."id" = 4 ORDER BY "saas_admins"."id" ASC LIMIT 1
Account Load (0.1ms) SELECT "accounts".* FROM "accounts" WHERE (accounts.deleted_at IS NULL) AND "accounts"."id" = $1 LIMIT 1 [["id", "3"]]
(0.1ms) BEGIN
Subscription Load (0.3ms) SELECT "subscriptions".* FROM "subscriptions" WHERE (subscriptions.deleted_at IS NULL) AND "subscriptions"."subscriber_id" = $1 AND "subscriptions"."subscriber_type" = $2 LIMIT 1 [["subscriber_id", 3], ["subscriber_type", "Account"]]
SQL (0.3ms) UPDATE "subscriptions" SET deleted_at = '2014-10-27 17:38:48.909001', deleted_at = '2014-10-27 17:38:48.909025' WHERE (subscriptions.deleted_at IS NULL) AND "subscriptions"."id" = 3
PG::SyntaxError: ERROR: multiple assignments to same column "deleted_at"
: UPDATE "subscriptions" SET deleted_at = '2014-10-27 17:38:48.909001', deleted_at = '2014-10-27 17:38:48.909025' WHERE (subscriptions.deleted_at IS NULL) AND "subscriptions"."id" = 3
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 7ms
ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR: multiple assignments to same column "deleted_at"
: UPDATE "subscriptions" SET deleted_at = '2014-10-27 17:38:48.909001', deleted_at = '2014-10-27 17:38:48.909025' WHERE (subscriptions.deleted_at IS NULL) AND "subscriptions"."id" = 3):
activerecord (4.0.4) lib/active_record/connection_adapters/postgresql_adapter.rb:791:in `async_exec'
Additionally, the sql is generated by act as paranoid gem, so we don't write the finder. We do account.destroy, the rest goes by dependecies. Here's the link to the gem if helpful: https://github.com/ActsAsParanoid/acts_as_paranoid
What can we do to fix this?
See the full logs below by visiting the Gist link: https://gist.github.com/rmagnum2002/2358536587bb34dbbc52

Rails sql issue with a simple where statement

I am trying to get a very simple statement working.
Node.where("nodeid = ?", nstart).select('id')
Results in
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WpSpzLzaFUx8QgFbwEfygQUkkqvbUgZl8Hh0UxJvT8E=", "edge"=>{"kind"=>"IsA", "start_id"=>"blabla", "end_id"=>"bliblib", "property1"=>"bloblbo"}, "commit"=>"Create Edge"}
(0.0ms) begin transaction
Node Load (0.1ms) SELECT "nodes".* FROM "nodes" WHERE "nodes"."id" = ? LIMIT 1 [["id", 0]]
CACHE (0.0ms) SELECT "nodes".* FROM "nodes" WHERE "nodes"."id" = ? LIMIT 1 [["id", 0]]
(0.0ms) rollback transaction
It should be just a ´select nodes.id from nodes where nodeid = blabla´ The limit doesn't matter.
However if I add .first.
Node.where("nodeid = ?", nstart).select('id').first
I get
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WpSpzLzaFUx8QgFbwEfygQUkkqvbUgZl8Hh0UxJvT8E=", "edge"=>{"kind"=>"IsA", "start_id"=>"blabla", "end_id"=>"bliblib", "property1"=>"bloblbo"}, "commit"=>"Create Edge"}
Node Load (0.1ms) SELECT "nodes"."id" FROM "nodes" WHERE (nodeid = 'blabla') ORDER BY "nodes"."id" ASC LIMIT 1
Node Load (0.1ms) SELECT "nodes"."id" FROM "nodes" WHERE (nodeid = 'bliblib') ORDER BY "nodes"."id" ASC LIMIT 1
(0.0ms) begin transaction
Node Load (0.1ms) SELECT "nodes".* FROM "nodes" WHERE "nodes"."id" = ? LIMIT 1 [["id", 0]]
CACHE (0.0ms) SELECT "nodes".* FROM "nodes" WHERE "nodes"."id" = ? LIMIT 1 [["id", 0]]
(0.1ms) rollback transaction
The first select now is what it should be but the follow up is again like before and seems to determine the eventual return value (because it doesn't work either). I just want the id when I only know the nodeid which basically is the name of the node.
What is going on in Rails here?
If you just want the id value you could do this
Node.where("nodeid = ?", nstart).limit(1).pluck(:id).first
This would return 1 integer with the value
EDIT:
ok scratch that, i guess you don't really need to use limit so a simple first would just do
Node.where("nodeid = ?", nstart).first[:id]
or
Node.where("nodeid = ?", nstart).first.id
You're making a query:
Node.where("nodeid = ?", nstart).select('id')
will return an array of objects (or empty array if there's no suitable records) *not array actually. Activerecord::Relation
If you use 'first' like Mohammad AbuShady told:
Node.where("nodeid = ?", nstart).select('id').first
you get an object (or nil if there's no record)
If you want an integer id value, you can get it from the object
Node.where("nodeid = ?", nstart).first.id
select('id') tells active record not to retrieve all the fields in the table, just 'id'

I can't save roles to my devise user table (for cancan)

I have installed Devise and CanCan, both which appear to be working. I have the column "role" in migrations but when I check the log after adding a user, the BD query shows no sign of adding a role
The full thing looks like this:
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"S+SjhxsALMbHkRBPPwOMIvHo1Bd0cNYl1g=", "user"=>{"email"=>"david#mail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "role"=>"receptionist"}, "commit"=>"Sign up"}
Unpermitted parameters: role
(0.2ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'david#mail.com' LIMIT 1
Binary data inserted for `string` type on column `encrypted_password`
SQL (3.1ms) INSERT INTO "users" ("created_at", "email", "encrypted_password", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Thu, 24 Oct 2013 08:52:31 UTC +00:00], ["email", "david#mail.com"], ["encrypted_password", "$2a$10$e3hKTibsZ7qSgOC5OelKgjumU8ufu46xNrBkmXcDEpix/m"], ["updated_at", Thu, 24 Oct 2013 08:52:31 UTC +00:00]]
(0.7ms) commit transaction
(0.1ms) begin transaction
Binary data inserted for `string` type on column `last_sign_in_ip`
Binary data inserted for `string` type on column `current_sign_in_ip`
SQL (0.7ms) UPDATE "users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?, "last_sign_in_ip" = ?, "current_sign_in_ip" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "users"."id" = 2 [["last_sign_in_at", Thu, 24 Oct 2013 08:52:31 UTC +00:00], ["current_sign_in_at", Thu, 24 Oct 2013 08:52:31 UTC +00:00], ["last_sign_in_ip", "127.0.0.1"], ["current_sign_in_ip", "127.0.0.1"], ["sign_in_count", 1], ["updated_at", Thu, 24 Oct 2013 08:52:31 UTC +00:00]]
(0.6ms) commit transaction
Redirected to http://0.0.0.0:3000/
Completed 302 Found in 108ms (ActiveRecord: 5.6ms)
Additionally, if I edit user and add the role I get the following error in the logs
Unpermitted parameters: role
I feel like the migrations may not have worked.. but I can access the role variable.. so I don't fully understand.
I'm fairly new to rails, so I'm expecting (and hoping) I've omitted something obvious.
Thanks in advance
From the looks of it that error Unpermitted parameters:role means you haven't whitelisted this in your application. I am aware that your there is a change in your application in handling the attribute protection. So you need to whitelist this parameter. If this was rails 4 you would do something like this:
class User < ActionController::Base
def create
User.create(params[:user])
end
private
def user_params
params.require(:person).permit(:first_name, :last_name, :password, :role)
end
end
The above is a change that comes with Rails 4. However in your case (rails 3) I think you just need to make this attribute accessible. So you could try and add the following to your User model.
attr_accessible :role
in your aplication controller
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:password, :password_confirmation,:current_password,:email,:name,:role) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:password, :password_confirmation,:email,:name,:role) }
end
it worked for me!

Rspec controller test hanging

Here is my controller test ("spec/controllers/api/tasks_controller_spec.rb")
require 'spec_helper'
describe Api::TasksController do
before :each do
#quadros = create :cat
#task = Task.new(:content => "Example task for #api")
#quadros.add_task(#task)
end
describe "GET index" do
it "returns a list of all user's tasks" do
get :index, :format => 'json'
expect(response).to eq(User.first.all_tasks)
end
end
end
Here is my Api::BaseController
class Api::BaseController < ApplicationController
respond_to :json
skip_before_filter :authenticate_user!
end
And Api::TasksController
class Api::TasksController < Api::BaseController
def index
#tasks = User.first.all_tasks
respond_with #tasks.to_json
end
end
My other test run fine.
When I run the api test, it executes the before block, makes the request as json, and then hangs on this query:
Processing by Api::TasksController#index as JSON
User Load (0.3ms) SELECT `users`.* FROM `users` LIMIT 1
Tag Load (0.3ms) SELECT `tags`.* FROM `tags` WHERE (user_id = 418 AND parent_tag_id IS NOT NULL)
Tag Load (0.2ms) SELECT `tags`.* FROM `tags` WHERE `tags`.`id` IN (NULL)
Tag Load (0.3ms) SELECT `tags`.* FROM `tags` WHERE (user_id = 418 AND parent_tag_id IS NULL)
Task Load (0.7ms) SELECT tasks.* FROM `tasks` JOIN tag_tasks on tasks.id = tag_tasks.task_id WHERE (tag_tasks.tag_id IN (301) OR creator_id = 418) GROUP BY tasks.id ORDER BY tasks.created_at DESC
Completed 200 OK in 99ms (Views: 0.1ms | ActiveRecord: 0.0ms)
User Load (0.3ms) SELECT `users`.* FROM `users` LIMIT 1
Tag Load (0.2ms) SELECT `tags`.* FROM `tags` WHERE (user_id = 418 AND parent_tag_id IS NOT NULL)
Tag Load (0.2ms) SELECT `tags`.* FROM `tags` WHERE `tags`.`id` IN (NULL)
Tag Load (0.2ms) SELECT `tags`.* FROM `tags` WHERE (user_id = 418 AND parent_tag_id IS NULL)
Task Load (0.7ms) SELECT tasks.* FROM `tasks` JOIN tag_tasks on tasks.id = tag_tasks.task_id WHERE (tag_tasks.tag_id IN (301) OR creator_id = 418) GROUP BY tasks.id ORDER BY tasks.created_at DESC
Where it will just sit forever.
Any ideas why this might be happening?
I've come across a similar issue, and it appears the problem is with your expectation line:
expect(response).to eq(User.first.all_tasks)
This is not how RSpec wants you to test the response body. Notice that in the docs, specialized matchers are used instead of the equality matcher:
expect(response).to render_template("index")
So the response object, which is an ActionController::TestResponse, is meant to be queried about what happened, not what the response body was. So your test should be something like:
expect(JSON.parse(response.body)).to eq(User.first.all_tasks)
(Note that the response body is a string.)
As for the explanation of why the test hangs—instead of outright failing—it appears that this block of code (lib/rspec/expectations/fail_with.rb:22 in the rspec-expectations gem version 2.14.0) is the culprit:
if actual && expected
if all_strings?(actual, expected)
if any_multiline_strings?(actual, expected)
message << "\nDiff:" << differ.diff_as_string(coerce_to_string(actual), coerce_to_string(expected))
end
elsif no_procs?(actual, expected) && no_numbers?(actual, expected)
message << "\nDiff:" << differ.diff_as_object(actual, expected)
end
end
The all_strings?, any_multiline_strings?, no_procs?, and no_numbers? methods (defined in the same file) all invoke args.flatten on [actual, expected]. In this case, from what I can tell the problem is that actual is a TestResponse, which causes the flatten method itself to hang without raising a runtime error. I didn't have time to investigate this further, but the source for Array.flatten is here if anyone is interested.