active-relation

Is there a DRY way to pass bind variables to an AR Relation?

荒凉一梦 提交于 2021-01-26 19:23:52
问题 I have code like this: t = "%#{term}%" where('name like ? or email like ? or postcode like ?', t, t, t) As you can see it's performing a search across several fields. Is there a way to avoid the duplicated t? It's making me feel dirty. Thanks 回答1: You can do it with a named placeholder: where('name LIKE :name OR email LIKE :name OR postcode LIKE :name', :name => t) This is often the best way to repeat a singular value several times in a query. 回答2: t = "%#{term}%" where('name || email ||

ActiveRecord Association select counts for included records

放肆的年华 提交于 2020-01-12 04:38:08
问题 Example class User has_many :tickets end I want to create association which contains logic of count tickets of user and use it in includes (user has_one ticket_count) Users.includes(:tickets_count) I tried has_one :tickets_count, :select => "COUNT(*) as tickets_count,tickets.user_id " ,:class_name => 'Ticket', :group => "tickets.user_id", :readonly => true User.includes(:tickets_count) ArgumentError: Unknown key: group In this case association query in include should use count with group by .

Rails :include doesn't include

邮差的信 提交于 2020-01-06 08:18:36
问题 My models: class Contact < ActiveRecord::Base has_many :addresses has_many :emails has_many :websites accepts_nested_attributes_for :addresses, :emails, :websites attr_accessible :prefix, :first_name, :middle_name, :last_name, :suffix, :nickname, :organization, :job_title, :department, :birthday, :emails_attributes end class Email < ActiveRecord::Base belongs_to :contact validates_presence_of :account validates_format_of :account, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on =>

Matching nested model association attribute with includes

…衆ロ難τιáo~ 提交于 2020-01-02 04:08:31
问题 Suppose I have the following models: class Post < ActiveRecord::Base has_many :authors class Author < ActiveRecord::Base belongs_to :post And suppose the Author model has an attribute, name . I want to search for all posts with a given author "alice", by that author's name. Say there is another author "bob" who co-authored a post with alice. If I search for the first result using includes and where : post = Post.includes(:authors).where("authors.name" => "alice").first You'll see that the

Rails: ActiveRecord where vs merge

心不动则不痛 提交于 2019-12-23 06:58:13
问题 First, I am getting the review statuses between particular dates. date_range = Date.parse(@from_date).beginning_of_day..Date.parse(@to_date).end_of_day @review_statuses = ReviewStatus.where(updated_at: date_range) Next, I need to apply an 'AND' condition. @review_cycle = params[:review_cycle] if @review_cycle.present? @review_statuses = @review_statuses.merge( ReviewStatus.where(evidence_cycle: @review_cycle) .or(ReviewStatus.where(roc_cycle: @review_cycle))) end Now for the below should I

Difference between ActiveRecord and ActiveRecord::Relation objects

半腔热情 提交于 2019-12-21 14:51:32
问题 I have searched but not able to find the brief explanation for the difference between ActiveRecord and ActiveRecord::relation object. I have understand that ActiveRecord is the single object find by something like User.find(1) And ActiveRecord::Relation is the array like object Find by something like User.where(id: 1) I am looking for the difference between them in terms of query execution or deep explanation about them, so it will clear the whole concept behind it. Thanks in advance! 回答1: An

How do I get Rails to eager load counts?

三世轮回 提交于 2019-12-17 23:46:58
问题 This is related to a question a year and change ago. I put up an example of the question that should work out of the box, provided you have sqlite3 available: https://github.com/cairo140/rails-eager-loading-counts-demo Installation instructions (for the main branch) git clone git://github.com/cairo140/rails-eager-loading-counts-demo.git cd rails-eager-loading-counts-demo rails s I have a fuller write-up in the repository, but my general question is this. How can I make Rails eager load counts

How will ActiveRelation affect rails' includes() 's capabilities?

瘦欲@ 提交于 2019-12-07 01:24:52
问题 I've looked over the Arel sources, and some of the activerecord sources for Rails 3.0, but I can't seem to glean a good answer for myself as to whether Arel will be changing our ability to use includes(), when constructing queries, for the better. There are instances when one might want to modify the conditions on an activerecord :include query in 2.3.5 and before, for the association records which would be returned. But as far as I know, this is not programmatically tenable for all :include

Turn SQL query into ActiveRecord Relation

我的未来我决定 提交于 2019-12-05 20:25:52
问题 How can I turn the following SQL query into an ActiveRecord relation so that I can expand on it with scopes? WITH joined_table AS ( SELECT workout_sets.weight AS weight, workouts.user_id AS user_id, workouts.id AS workout_id, workout_sets.id AS workout_set_id, workout_exercises.exercise_id AS exercise_id FROM workouts INNER JOIN workout_exercises ON workout_exercises.workout_id = workouts.id INNER JOIN workout_sets ON workout_sets.workout_exercise_id = workout_exercises.id ORDER BY workout

Ruby on Rails - How to join two tables?

本小妞迷上赌 提交于 2019-12-04 14:37:18
问题 I have two tables (subjects and pages) in one-to-many relations. I want to add criterias from subjects as well pages to parse a sql, but the progress has been very slow and often times running into problems. I'm brand new in rails, please help. class Subject < ActiveRecord::Base has_many :pages end class Page < ActiveRecord::Base belongs_to :subject end sample data in subjects, listed three columns below: id name level 1 'Math' 1 6 'Math' 2 ... Sample data in pages, listed columns below: id