activerecord

Is it possible to inner join across multiple databases in Rails?

空扰寡人 提交于 2019-12-11 04:49:26
问题 I'm having difficult accessing data with a has_many :through association where some of the tables live in a separate database. # database_one class Input < ApplicationRecord belongs_to :user # Works great end # database_two class User < AbstractClass belongs_to :group # Works great has_many :inputs # Works great end # database_two class Group < AbstractClass has_many :users # Works great has_many :inputs, through: :users # Does not work at all end class AbstractClass < ApplicationRecord self

Ruby delegating class/relation-level methods

我只是一个虾纸丫 提交于 2019-12-11 04:49:02
问题 EDIT -- I changed the object-oriented modeling to better reflect the not-particularly-intuitive relationship in my app, (thanks, Anthony) so that this question makes more sense. Sorry about that. In my Rails app, I want certain models to be able to delegate to other models on not just an instance level but also a class/relation level. That is to say, assuming a House model, which has_many Users and defines a class-level method " addresses " that's called on relations of houses , I could do

Why is ActiveRecord destroy_all taking so long?

大兔子大兔子 提交于 2019-12-11 04:48:53
问题 I have a simple rails app with articles and comments running on MySQL 5.5, Ruby 1.9.3 and rails 3.2.12: class Article < ActiveRecord::Base attr_accessible :body, :title has_many :comments end class Comment < ActiveRecord::Base attr_accessible :content belongs_to :article end I have generated lots of comments for an article, and am now trying to delete them all in the rails console: $ rails c Loading development environment (Rails 3.2.12) [1] pry(main)> a = Article.find(1) (2.0ms) SET SQL_AUTO

How to make a MySQL query to return the ordered records on a “joins association” attribute/column?

为君一笑 提交于 2019-12-11 04:25:25
问题 I have models roughly as follows: class Article < ApplicationRecord has_many :writing_associations has_many :comments, :through => :writing_associations has_many :author_article_associations has_many :authors, :through => author_article_associations end class Comment < ApplicationRecord has_many :writing_associations has_many :articles, :through => :writing_associations has_many :author_comment_associations has_many :authors, :through => :author_comment_associations def self.articles

What is the source of “unknown OID” errors in Rails?

好久不见. 提交于 2019-12-11 04:25:08
问题 When replicating an app to production, my POSTGIS table columns started misbehaving, with Rails informing me there was an "unknown OID 26865" and that the fields would be treated as String. Instead of current_pos yielding e. g. #<RGeo::Geographic::SphericalPointImpl:0x22fabdc "POINT (13.39318248760133 52.52908798020595)"> I would get 0101000020E6100000FFDD958664C92A403619DEE6B2434A40 . It looked like the activerecord-postgis-adapter was not installed, or installed badly, but I eliminated that

SQL query to return nil for dates not present in the table

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 04:22:33
问题 I have a table 'my_table'. It has the following data : ID --- Date 1 --- 01/30/2012 2 --- 01/30/2012 3 --- 05/30/2012 I can write a SQL query to return the count of ID's between certain dates, grouped by month, like this : {"01/30/2012" => 2, "05/30/2012" => 1} How can I get a result which has all the missing months between the requested dates with value '0', like this : {"01/30/2012" => 2, "02/30/2012" => 0, "03/30/2012" => 0, "04/30/2012" => 0, "05/30/2012" => 1} Thanks in advance. 回答1: The

Rails transactions

可紊 提交于 2019-12-11 04:10:32
问题 I am trying to use ActiveRecord::Base.transaction . I figured that rollback doesn't work by default using Rails 1.2.6 and mysql 5.0. Playing with it a little bit more I found out that autocommit is not set to 0 in mysql connection. Questions: 1) How do I disable autocommit in rails for all connections? 2) Will it have some negative impact on the other code that doesn't have to be transactional? 回答1: If you have a mix of code that needs explicit transactions and code that can rely on

Updating rails has_many through relations

戏子无情 提交于 2019-12-11 04:06:51
问题 I have models class Agency < ActiveRecord::Base has_many :specializations has_many :cruise_lines, through: :specializations end class CruiseLine < ActiveRecord::Base has_many :specializations has_many :agencies, through: :specializations end class Specialization < ActiveRecord::Base belongs_to :agency, inverse_of: :specializations belongs_to :cruise_line, inverse_of: :specializations end I want to update Specialization collection (that is delete some of old relations and add a few new if

Rails pending migration in rake db:test:prepare

a 夏天 提交于 2019-12-11 04:03:38
问题 I've run rake db:migrate and all of my migrations ran. However, when I try to run rake db:test:prepare I get the error: You have 1 pending migrations: 20130724211328 CreateGalleries Run `rake db:migrate` to update your database then try again. Then running rake db:migrate again gives the error: PG::Error: ERROR: relation "galleries" already exists... But in the console I can create and manipulate the Gallery model exactly as shown in the CreateGalleries migration. The table is not being

How do I prevent Rails from creating a session?

我们两清 提交于 2019-12-11 03:59:57
问题 I've got a Rails app for which I'm storing sessions in the database using activerecord-session_store. I want to prevent the creation of sessions for users that aren't logged in, because right now every crawler that hits the front page of the site gets a session that persists in the DB. My app uses Rails 4.2.0, activerecord-session_store 0.1.1, and Devise 3.5.2. 回答1: Session creation is handled by Rack and the commit_session method in Rack::Session::Abstract::ID provides an option to drop the