activerecord

Rails query interface where clause issue?

六眼飞鱼酱① 提交于 2020-01-24 01:08:12
问题 i am try to run the sql query as follows @applicants = Applicant.where("applicants.first_name LIKE ? AND applicants.status = ?", "%#{people}%", ["new", "in-review"] ) I am getting an mysql error as : ActionView::Template::Error (Mysql2::Error: Operand should contain 1 column(s): SELECT `applicants`.* FROM `applicants` WHERE (applicants.first_name LIKE '%sh%' AND applicants.status = 'new','in-review')): can anyone help me in this thanks in advance 回答1: You have to use IN clause of mysql

Rails filter when param is present for large model

末鹿安然 提交于 2020-01-24 01:01:06
问题 This blog post explains how to filter based on the parameters that are present with this code. def index @products = Product.where(nil) # creates an anonymous scope @products = @products.status(params[:status]) if params[:status].present? @products = @products.location(params[:location]) if params[:location].present? @products = @products.starts_with(params[:starts_with]) if params[:starts_with].present? end The Product.where(nil) part of the solution is problematic because it will load

Rails 5: ActiveRecord collection index_by

…衆ロ難τιáo~ 提交于 2020-01-23 12:28:06
问题 Upgraded app to Rails 5 using the index method. The issue is that it is not incrementing to the next ActiveRecord collection record. The below code below use to work in Rails 4.0. Tried with index_by. def next_question index = campaign.quiz_questions.index self campaign.quiz_questions[index + 1] end Debugger (byebug) campaign.quiz_questions.index *** NoMethodError Exception: undefined method `index' for #<QuizQuestion::ActiveRecord_Associations_CollectionProxy:0x007f80012d71b0> Did you mean?

Using uniq in a has_and_belongs_to_many relationship in Rails 4

早过忘川 提交于 2020-01-23 11:03:26
问题 I'm trying to implement a unique constraint on a has_and_belongs_to_many relationship like so: class User has_and_belongs_to_many :foos, uniq: true end Because I only want unique foos when I call user.foos , I added the uniq option. Since upgrading to Rails 4 I've started to get the following warning: DEPRECATION WARNING: The following options in your User.has_and_belongs_to_many :foos declaration are deprecated: :uniq. Please use a scope block instead. For example, the following: has_many

Disable ActiveRecord in Ruby on Rails 5

99封情书 提交于 2020-01-23 02:41:54
问题 I am a newbie in Rails. I want to disable ActiveRecord in Rails 5. I've already found several answers Here, Here and Here But none of them seems working for me. Would you help me with this issue, please? Thanks. 回答1: Accepted answer in this post of mechanicalfish is right. But in my case, I've done following 2 additional things (I've used Rails 5.1.2) Delete model files in models directory models/application_record.rb models/widget.rb Hope this helps! 回答2: Just to update, there is no need to

How to rescue exception central and DRY?

…衆ロ難τιáo~ 提交于 2020-01-23 01:02:09
问题 I have an exception that is produced at ~20 seperate places. It can be rescued easy and in the same way at every place but thats not dry and eaven a crapy work! I want to rescue this exception at a central position. How can I arrange this? Its about the ActiveRecord::RecordNonUnique exception by the way,... 回答1: What about this ? def rescue_from_record_non_unique yield rescue ActiveRecord::RecordNonUnique # your code end # ... rescue_from_record_non_unique do # do something end 来源: https:/

Rspec Mocking: ActiveRecord::AssociationTypeMismatch

£可爱£侵袭症+ 提交于 2020-01-22 19:54:31
问题 I'm new to Rspec and trying to set up a test for a User Profile. Profile belongs_to User. Now, I have an API integration with a third party site that works through the User Model, but some of the information for that API link is contained in Profile, so I have an "after_update" filter on Profile that tells the parent user to save, which triggers an update of the API. I'm trying to write a test for this, and I'm getting an ActiveRecord::AssociationTypeMismatch. The reason is I'm using a mock

Return false and rollback in after_save callback

懵懂的女人 提交于 2020-01-22 18:50:14
问题 In ActiveRecord model after_save callback I need to ROLLBACK transaction and return false. def after_save_callback if mycondition? raise ActiveRecord::Rollback end end This callback rolls-back transaction but mymodel.save! returns true. How to make it return false and rollback? 回答1: I don't think you can do this with after_save You should be looking at around_save instead: def around_save ActiveRecord::Base.transaction do yield # calls the actual save method raise ActiveRecord::Rollback if my

Codeigniter LIKE with wildcard(%)

爷,独闯天下 提交于 2020-01-22 06:44:47
问题 I have simple database query in codeigniter, however I cannot get search to work with wildcard. This is my code: $this->db->like('film.title',"%$query%"); $this->db->escape_like_str($query); $res = $this->db->get('film'); If I remove wildcard(%) then search works fine. Also $query is just string with user input. Any help is appreciated. 回答1: $this->db->like() automatically adds the %s and escapes the string. So all you need is $this->db->like('title', $query); $res = $this->db->get('film');

Rails ignores constants in SQL SELECT statement

谁都会走 提交于 2020-01-21 19:50:27
问题 Is there a trick required to make Rails recognize constants in a SQL select statement? For example, the following SQL statement is valid: SELECT id, name, 1 AS constant FROM table_name And I would expect the results to have three columns returned: id , name and constant . The value in the constant column would always be 1. However, in Rails if I try to do the same thing the constant column gets dropped using Model.find_by_sql : TableName.find_by_sql("SELECT id, name, 1 AS constant FROM table