rails-activerecord

Rails ActiveRecord: lock without reload

假如想象 提交于 2020-01-07 00:58:50
问题 Can I lock an ActiveRecord object without reloading it? user = User.find(1) => #<User id: 1, name: "Ed" ...> user.name = "Joe" => "Joe" user.lock! => #<User id: 1, name: "Ed" ...> I could send a raw SQL "SELECT ... FOR UPDATE", but it doesn't seem too clean. 回答1: I ended up adding a method to ActiveRecord::Base : ActiveRecord::Base.class_eval do def lock_without_reload! self.class.lock(true).find(id) true end end 来源: https://stackoverflow.com/questions/38211871/rails-activerecord-lock-without

Rails ActiveRecord: lock without reload

↘锁芯ラ 提交于 2020-01-07 00:58:50
问题 Can I lock an ActiveRecord object without reloading it? user = User.find(1) => #<User id: 1, name: "Ed" ...> user.name = "Joe" => "Joe" user.lock! => #<User id: 1, name: "Ed" ...> I could send a raw SQL "SELECT ... FOR UPDATE", but it doesn't seem too clean. 回答1: I ended up adding a method to ActiveRecord::Base : ActiveRecord::Base.class_eval do def lock_without_reload! self.class.lock(true).find(id) true end end 来源: https://stackoverflow.com/questions/38211871/rails-activerecord-lock-without

Make a request with activerecord to get only the users from groups and not from others

家住魔仙堡 提交于 2020-01-06 06:46:35
问题 I'm trying to get users from few groups (with given ids) and exclude the users from other groups. I've tried something like : User.joins(:groups).where(groups: {id: ["8939","8950"]}).where.not(groups: {id: 8942}).map(&:id) User Load (0.9ms) SELECT "users".* FROM "users" INNER JOIN "groups_users" ON "groups_users"."user_id" = "users"."id" INNER JOIN "groups" ON "groups"."id" = "groups_users"."group_id" WHERE "groups"."id" IN (8939, 8950) AND "groups"."id" != $1 [["id", 8942]] => [119491,

Make a request with activerecord to get only the users from groups and not from others

对着背影说爱祢 提交于 2020-01-06 06:45:34
问题 I'm trying to get users from few groups (with given ids) and exclude the users from other groups. I've tried something like : User.joins(:groups).where(groups: {id: ["8939","8950"]}).where.not(groups: {id: 8942}).map(&:id) User Load (0.9ms) SELECT "users".* FROM "users" INNER JOIN "groups_users" ON "groups_users"."user_id" = "users"."id" INNER JOIN "groups" ON "groups"."id" = "groups_users"."group_id" WHERE "groups"."id" IN (8939, 8950) AND "groups"."id" != $1 [["id", 8942]] => [119491,

Rails only give records that “belong_to”

拜拜、爱过 提交于 2020-01-05 12:57:36
问题 I have a author model, a article model and a metric model. Author has many articles and article has many metrics. No in my articles_controller 's show action I have @articles = Article.where(author_id: params[:id]) which limits the articles to the ones by that author. Can I do something similar for the metrics? Is there a rails way to say something like @metrics = Metric.WHICHBELONGSTO(@articles) ? Alternatively can I pass an array of allowed IDs in? like @metrics = Metric.where(article_id is

Image name not inserted into database in ruby on rails

时光总嘲笑我的痴心妄想 提交于 2020-01-05 08:28:21
问题 I have a problem to store image name into database. image upload to folder working fine but image name will not saved into db Model code : class Post < ActiveRecord::Base attr_accessible :name, :imag attr_accessor :imag def self.save(upload) name = upload['imag'].original_filename directory = 'public/data' # render :text => directory # create the file path path = File.join(directory,name) # write the file File.open(path, "wb") { |f| f.write(upload['imag'].read)} end end Controller code: def

Rails mailer has issues with session persistence and with sending email

天涯浪子 提交于 2020-01-05 07:03:34
问题 I'm working through Ryan Bates' Railscast #124: Beta Invitations. I've got all the code in place, but I still two problems. Sessions don't persist through the email sending process. For some reason, when a signed in user sends an invite to a friend, the app signs out the user. Signed in users can't send email. I get an error message about having the wrong number of arguments. I don't think these two problems are related, but on the off chance they are, I thought I should mention both. Does

Rails mailer has issues with session persistence and with sending email

北战南征 提交于 2020-01-05 07:01:11
问题 I'm working through Ryan Bates' Railscast #124: Beta Invitations. I've got all the code in place, but I still two problems. Sessions don't persist through the email sending process. For some reason, when a signed in user sends an invite to a friend, the app signs out the user. Signed in users can't send email. I get an error message about having the wrong number of arguments. I don't think these two problems are related, but on the off chance they are, I thought I should mention both. Does

Testing Rails module without loading ActiveRecord

孤者浪人 提交于 2020-01-04 11:10:08
问题 I'm working on a Rails app which I am testing with RSpec. After a lot of waiting for tests to finish, I've followed Gary Bernhardt's advice and this informative post by Paul Annesley and setup a tiered spec_helper where I only load what parts of Rails I absolutely need, to keep test times down, and extract functionality into separate modules which I test in isolation. This is working up to a point. The problem is that I have a module (extending ActiveSupport::Concern ) with instance and class

How do you get a count of a subquery in Active Record?

回眸只為那壹抹淺笑 提交于 2020-01-04 06:33:24
问题 I'm migrating an SQL query to Active Record. Here is a simplified version: SELECT count(*) FROM ( SELECT type, date(created_at) FROM notification_messages GROUP BY type, date(created_at) ) x I'm not sure how to implement this in Active Record. This works, but it's messy: sql = NotificationMessage. select("type, date(created_at) AS period"). group("type", "period"). to_sql NotificationMessage.connection.exec_query("SELECT count(*) FROM (#{sql}) x") Another possibility is to do the count in