rails-activerecord

Rails rollback when creating user with nested forms

匆匆过客 提交于 2019-12-20 03:52:09
问题 I'm getting a rollback when trying to add a user to my DB and I'm not sure why. I have three models: company.rb class Company < ActiveRecord::Base acts_as_paranoid has_many :locations, dependent: :destroy has_many :users, dependent: :destroy has_many :tests, through: :locations has_many :reports, dependent: :destroy accepts_nested_attributes_for :locations, :users validates_presence_of :name end ** user.rb ** class User < ActiveRecord::Base acts_as_paranoid devise :database_authenticatable,

Do shoulda-matchers' ActiveRecord matchers violate the “test behavior not implementation” rule?

久未见 提交于 2019-12-19 22:09:12
问题 For example, if I am using should validate_presence_of in my spec, that's only testing that I have the validate_presence_of piece of code inside my model, and that's testing implementation. More importantly, isn't that spec totally useless for testing the real problem, which is "if I don't fill out a certain field, will the model be saved successfully?" 回答1: Some of shoulda-matchers' matchers don't test implementation, they test behavior. For example, look at the source for allow_value (which

Rails 4 Relation#all deprecation

↘锁芯ラ 提交于 2019-12-19 14:58:08
问题 In my app I created a recent posts feature. @recentposts = Post.all(:order => 'created_at DESC', :limit => 5) This variable makes some trouble. When I run tests I have the following error: DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. Post.where(published: true).load ). If you want to get an array of records from a relation, you can call #to_a (e.g. Post.where(published: true).to_a ). (called from show at /home/mateusz/rails4

Dynamically create query - Rails 5

送分小仙女□ 提交于 2019-12-19 12:01:52
问题 If I manually write a query, it will be like User.where("name LIKE(?) OR desc LIKE(?)",'abc','abc') .where("name LIKE(?) OR desc LIKE(?)",'123','123') However, I need to dynamically generate that query. I am getting data like def generate_query(the_query) query,keywords = the_query # Here # query = "name LIKE(?) OR desc LIKE(?)" # keywords = [['abc','abc'],['123','123']] keywords.each do |keyword| users = User.where(query,*keyword) <-- not sure how to dynamically add more 'where' conditions.

building a simple search form in Rails?

喜你入骨 提交于 2019-12-19 10:59:08
问题 I'm trying to build a simple search form in Ruby on Rails, my form is simple enough basically you select fields from a series of options and then all the events matching the fields are shown. The problem comes when I leave any field blank. Here is the code responsible for filtering the parameters Event.joins(:eventdates).joins(:categories).where ("eventdates.start_date = ? AND city = ? AND categories.name = ?", params[:event][:date], params[:event][:city], params[:event][:category]).all From

How do I disable the migrations feature in a Rails app?

馋奶兔 提交于 2019-12-19 04:22:07
问题 Background We engineer database models and application models separately (RDMBS architects vs OOP engineers). From what I've seen regarding Rails versus domain/key normal form, Rails migrations cannot easily duplicate all the features of a well-designed enterprise RDBMS (if at all) so we don't migrate and instead use other tools to build databases (nevermind the problem of object-relational impedance mismatch). Data integrity and DB performance are too valuable to us to risk RDBMS model

How do I disable the migrations feature in a Rails app?

强颜欢笑 提交于 2019-12-19 04:21:10
问题 Background We engineer database models and application models separately (RDMBS architects vs OOP engineers). From what I've seen regarding Rails versus domain/key normal form, Rails migrations cannot easily duplicate all the features of a well-designed enterprise RDBMS (if at all) so we don't migrate and instead use other tools to build databases (nevermind the problem of object-relational impedance mismatch). Data integrity and DB performance are too valuable to us to risk RDBMS model

Rails ActiveRecord handle an id column that is not the primary key

梦想与她 提交于 2019-12-19 04:07:45
问题 Struggling with ActiveRecord auto assigning the :id attribute as the primary key even though it is a separate column. Table - legacy-table id - int pk_id - int (primary key) name - varchar2 info - varchar2 Model class LegacyModel < ActiveRecord::Base self.table_name = 'legacy-table' self.primary_key = 'pk_id' default_scope {order(:name => :asc)} alias_attribute :other_id, :id end I don't care that ActiveRecord automatically assigns the primary key (pk_id) to the :id attribute however I lose

Rails isn't running destroy callbacks for has_many through join model

∥☆過路亽.° 提交于 2019-12-18 16:46:14
问题 I have two AR models and a third has_many :through join model like this: class User < ActiveRecord::Base has_many :ratings has_many :movies, through: :ratings end class Movie < ActiveRecord::Base has_many :ratings has_many :users, through: :ratings end class Rating < ActiveRecord::Base belongs_to :user belongs_to :movie after_destroy do puts 'destroyed' end end Occasionally, a user will want to drop a movie directly (without directly destroying the rating). However, when I do: # puts user

Heroku + Sidekiq: ActiveRecord::StatementInvalid: PG::UnableToSend: SSL SYSCALL error: EOF detected

跟風遠走 提交于 2019-12-18 14:13:04
问题 Hi we are running on Heroku's Cedar stack with Unicorn and Sidekiq. We intermittently get the following errors BurnThis ActiveRecord::StatementInvalid: PG::UnableToSend: SSL SYSCALL error: EOF detected ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() SSL SYSCALL error: Connection timed out Does anyone have any insight what the direct cause of these errors? Is it too many connections to our database? We have our forking set up already in the following way: unicorn.rb worker