rails-activerecord

PostgreSQL ilike with multiple matches in Rails ActiveRecord

北城以北 提交于 2021-02-18 06:08:27
问题 I am trying to retrieve multiple records from my DB with the following query: User.where('name ilike ?','%thomas%') this works fine. Now I want to retrieve multiple records at the same time and tried this (which seems to be syntactically incorrect): User.where('name ilike any',['%thomas%','%james%','%martin%']) What am I doing wrong? So just to clarify: I want to retrieve all records that match one of the names, so its an OR statement I am looking for. 回答1: You can do it by User.where('name

PostgreSQL ilike with multiple matches in Rails ActiveRecord

ⅰ亾dé卋堺 提交于 2021-02-18 06:07:44
问题 I am trying to retrieve multiple records from my DB with the following query: User.where('name ilike ?','%thomas%') this works fine. Now I want to retrieve multiple records at the same time and tried this (which seems to be syntactically incorrect): User.where('name ilike any',['%thomas%','%james%','%martin%']) What am I doing wrong? So just to clarify: I want to retrieve all records that match one of the names, so its an OR statement I am looking for. 回答1: You can do it by User.where('name

PostgreSQL ilike with multiple matches in Rails ActiveRecord

最后都变了- 提交于 2021-02-18 06:07:33
问题 I am trying to retrieve multiple records from my DB with the following query: User.where('name ilike ?','%thomas%') this works fine. Now I want to retrieve multiple records at the same time and tried this (which seems to be syntactically incorrect): User.where('name ilike any',['%thomas%','%james%','%martin%']) What am I doing wrong? So just to clarify: I want to retrieve all records that match one of the names, so its an OR statement I am looking for. 回答1: You can do it by User.where('name

Rails first_or_create ActiveRecord method

我怕爱的太早我们不能终老 提交于 2021-02-17 18:46:21
问题 What does the first_or_create / first_or_create! method do in Rails? According to the documentation, the method " has no description "... 回答1: From the Guides first_or_create The first_or_create method checks whether first returns nil or not. If it does return nil, then create is called. This is very powerful when coupled with the where method. Let’s see an example. Suppose you want to find a client named ‘Andy’, and if there’s none, create one and additionally set his locked attribute to

Rails first_or_create ActiveRecord method

做~自己de王妃 提交于 2021-02-17 18:40:30
问题 What does the first_or_create / first_or_create! method do in Rails? According to the documentation, the method " has no description "... 回答1: From the Guides first_or_create The first_or_create method checks whether first returns nil or not. If it does return nil, then create is called. This is very powerful when coupled with the where method. Let’s see an example. Suppose you want to find a client named ‘Andy’, and if there’s none, create one and additionally set his locked attribute to

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 ||

Find user who has no post in Rails

匆匆过客 提交于 2021-01-23 08:37:41
问题 This the the database relation: class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end I come across a functionality where I want to query all the users who don't have any posts yet. I know that we can do this with something like this: users = User.all users.each do |user| unless user.posts.any? # do something when user don't have any post. end end However, I wonder if there is any way to optimize this by using one query only. Thanks! 回答1:

Find user who has no post in Rails

人走茶凉 提交于 2021-01-23 08:34:48
问题 This the the database relation: class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end I come across a functionality where I want to query all the users who don't have any posts yet. I know that we can do this with something like this: users = User.all users.each do |user| unless user.posts.any? # do something when user don't have any post. end end However, I wonder if there is any way to optimize this by using one query only. Thanks! 回答1:

Find user who has no post in Rails

懵懂的女人 提交于 2021-01-23 08:33:07
问题 This the the database relation: class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end I come across a functionality where I want to query all the users who don't have any posts yet. I know that we can do this with something like this: users = User.all users.each do |user| unless user.posts.any? # do something when user don't have any post. end end However, I wonder if there is any way to optimize this by using one query only. Thanks! 回答1:

PG::ForeignKeyViolation: ERROR: update or delete on table “xxx” violates foreign key constraint

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-29 12:15:21
问题 I have several tables that have foreign key constraints associated with them, each referencing the other in a hierarchical fashion as outlined below. When I try to destroy a Company that has at least 1 Project, that has at least 1 Task, that has at least 1 TaskTime like so... irb(main):014:0> Company.first.destroy I get the below output and error. I am under the impression now that simply having dependent: :delete_all doesn't deal with the foreign key constraints, is this true? If so, how do