activerecord

Merge record into ActiveRecord Relation

╄→尐↘猪︶ㄣ 提交于 2020-01-25 06:34:05
问题 I pull a record as: def self.imp_broadcast_preview! Broadcast.where(for_gamers: true).order(:created_at).last end And then in my controller I have: def index @conversations = Conversation.where(gamer: @gamer) @conversations << Broadcast.imp_broadcast_preview! end The above code works properly in Rails 4.2 and merges the last broadcast message in the conversations. I just updated my codebase to Rails 5.2 and now I am getting an error: NoMethodError (undefined method `<<' for #<Conversation:

Converting a simple query into a tricky named scope in RoR

霸气de小男生 提交于 2020-01-25 04:43:14
问题 I have a simple scoping question. I would like to do this as a scope: if article.responses.blank? return false elsif article.responses.last.passed.eql?(false) return true else return false end So on the article model I would have something like this: scope :failed_response, { :joins=>[:responses], :conditions=>["responses.passed = ?", false] } The problem is, I only want instances where the most recent response failed. I'm sure these is a way to do this with either fancy sorting or some kind

Factory Girl has_many through validation deadlock

江枫思渺然 提交于 2020-01-25 03:03:26
问题 I have a Listing model which has_many :categories, through: :categories_listings and has_many :categories_listings . I was testing it with a Factory Girl factory which looks like this: factory :listing do |f| f.sequence (:title) { |n| "Listing #{n}" } message {Faker::Lorem.paragraph} cards {[FactoryGirl.create(:card)]} categories {[FactoryGirl.create(:category)]} association :delivery_condition association :unit_of_measure quantity 1 unit_price 1 end And everything was OK until I added the

how to localize the active record error messages

给你一囗甜甜゛ 提交于 2020-01-24 12:56:04
问题 I try to figure out how I can localize the error item name in my rails application which appear when a user sign's up with uncorrect datas... I figured out how to override the messages but not the names of the messages like ("password", "login", "email", ...) de: activerecord: errors: models: user: attributes: password_confirmation: blank: "" password: confirmation: "" blank: "" too_short: "" login: taken: "" blank: "" too_short: "" email: taken: "" blank: "" too_short: "" invalid: ""

rspec - how to check that allow_blank exists

不羁的心 提交于 2020-01-24 11:06:29
问题 I have a test for uniqueness which works: it { should validate_uniqueness_of(:name).case_insensitive } however when I try to do a similar test for name it fails it { should validate_presence_of(:name).allow_blank } I get this error: undefined method allow_blank'` 回答1: According to this list, there's no method in shoulda that can be chained to validate_presence_of that would support what you want to do. Have a look at the source code of the matcher here. However, there is another matcher you

Rails how to handle error and exceptions in model

一个人想着一个人 提交于 2020-01-24 08:55:47
问题 So I'm parsing data from twitter api in rails using the twitter library, and sometimes the response from api might be like this: { error: "Invalid parameter" } And the model will raise an exception, right now I'm silently catch it and put the error.message into the log, how do I pass this exception to the controller so I can display it on the view? Thanks. UPDATE: The error is likely to happen because I'm allowing my customer to build queries, and they might put advanced queries like "https:/

Rails how to handle error and exceptions in model

空扰寡人 提交于 2020-01-24 08:55:36
问题 So I'm parsing data from twitter api in rails using the twitter library, and sometimes the response from api might be like this: { error: "Invalid parameter" } And the model will raise an exception, right now I'm silently catch it and put the error.message into the log, how do I pass this exception to the controller so I can display it on the view? Thanks. UPDATE: The error is likely to happen because I'm allowing my customer to build queries, and they might put advanced queries like "https:/

find_or_create race conditions

家住魔仙堡 提交于 2020-01-24 03:25:28
问题 I'm trying to use ActiveRecord's find_or_create_by_*column* , but I'm getting errors from Postgres letting me know that it occasionally fails to find the model, and tries to insert one anyways. It's really important that I keep this table unique, so I added a :unique => true attribute to its migration, so that Postgres would know that I was serious about it. And, fail: ActiveRecord::StatementInvalid: PGError: ERROR: duplicate key value violates unique constraint "index_marketo_leads_on_person

find_or_create race conditions

孤街浪徒 提交于 2020-01-24 03:25:05
问题 I'm trying to use ActiveRecord's find_or_create_by_*column* , but I'm getting errors from Postgres letting me know that it occasionally fails to find the model, and tries to insert one anyways. It's really important that I keep this table unique, so I added a :unique => true attribute to its migration, so that Postgres would know that I was serious about it. And, fail: ActiveRecord::StatementInvalid: PGError: ERROR: duplicate key value violates unique constraint "index_marketo_leads_on_person

How can I get all list of scopes in ActiveRecord 3.x

旧城冷巷雨未停 提交于 2020-01-24 02:50:07
问题 I am using ActiveRecord with Rails 3. I defined scopes in my model. How can I get the list of all scopes of that model? Previously I could use Model.scopes OR Can I check a scope is defined or not? Something like Model.scope_defined?("scope_name") Thanks in advance. 回答1: You can see if a scope is defined or not this way Model.send(:valid_scope_name?, :scope_name) it will return true if it does exist and nil if it does not. If you check the source code of valid_scope_name?, you see that you