rails-activerecord

Ruby on Rails named class property of model

泄露秘密 提交于 2020-01-25 10:10:22
问题 I have two classes Product and user which have a has_many relationship with a polymorphic type as such. class User < ActiveRecord::Base has_many :pictures, as: :imageable end class Product < ActiveRecord::Base has_many :pictures, as: :imageable end class Picture < ActiveRecord::Base belongs_to :imageable, polymorphic: true end I want to also add a new property to the user models of profile_picture so @user.profile_picture will return a picture object. how can this be achieved? In particular

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

Rails ignores constants in SQL SELECT statement

血红的双手。 提交于 2020-01-21 19:49:09
问题 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

Rails 4 active record validation - conditionally validate presence of 4 attributes if at least one is present while allowing none to be present

末鹿安然 提交于 2020-01-16 18:07:07
问题 I have a form with 10 attributes. Among them I have 4 attributes which I need to apply what I'd call a"mutually conditional presence" Active Record validation. I want that (A) if one at least is present, then the other 3 must be present (B) still allow none to be present (if the other 3 are blank, then the fourth has the right be be blank) Here are the four attributes: address_line_1 zipcode state country It means that if the user fills ONE of them then ALL the others have to be present. But

Rails 4 active record validation - conditionally validate presence of 4 attributes if at least one is present while allowing none to be present

断了今生、忘了曾经 提交于 2020-01-16 18:07:07
问题 I have a form with 10 attributes. Among them I have 4 attributes which I need to apply what I'd call a"mutually conditional presence" Active Record validation. I want that (A) if one at least is present, then the other 3 must be present (B) still allow none to be present (if the other 3 are blank, then the fourth has the right be be blank) Here are the four attributes: address_line_1 zipcode state country It means that if the user fills ONE of them then ALL the others have to be present. But

Selecting all columns with Inner Join in Active Record

廉价感情. 提交于 2020-01-16 03:42:07
问题 In the Ruby on Rails Guide I see examples using Joins. For example Category.joins(:posts) results in the query SELECT categories.* FROM categories INNER JOIN posts ON posts.category_id = categories.id This is all well and good, but how can I get returned both the categories AND posts columns using Active Record? Or am I totally missing something with SQL? 回答1: You need to call #includes : Category.includes(:posts) 来源: https://stackoverflow.com/questions/14947820/selecting-all-columns-with

Selecting all columns with Inner Join in Active Record

天大地大妈咪最大 提交于 2020-01-16 03:42:04
问题 In the Ruby on Rails Guide I see examples using Joins. For example Category.joins(:posts) results in the query SELECT categories.* FROM categories INNER JOIN posts ON posts.category_id = categories.id This is all well and good, but how can I get returned both the categories AND posts columns using Active Record? Or am I totally missing something with SQL? 回答1: You need to call #includes : Category.includes(:posts) 来源: https://stackoverflow.com/questions/14947820/selecting-all-columns-with

Cannot programmatically combine AND and OR conditions using Arel

白昼怎懂夜的黑 提交于 2020-01-15 10:34:12
问题 Given the SQL conditions cond1, cond2 and cond3 generated using Arel operators (.eq for example), I cannot seem to use Arel to produce the SQL: SELECT * FROM <table> WHERE (cond1 AND cond2) OR cond3 This is because to AND conditions together you use .where(), but you can't then .or() the result of .where(). You can only .or() conditions together inside a .where(). I.e. .where and .or are not on the same "level", I would guess one needs a dedicated .and() method on the same level as .or().

Cannot programmatically combine AND and OR conditions using Arel

a 夏天 提交于 2020-01-15 10:31:13
问题 Given the SQL conditions cond1, cond2 and cond3 generated using Arel operators (.eq for example), I cannot seem to use Arel to produce the SQL: SELECT * FROM <table> WHERE (cond1 AND cond2) OR cond3 This is because to AND conditions together you use .where(), but you can't then .or() the result of .where(). You can only .or() conditions together inside a .where(). I.e. .where and .or are not on the same "level", I would guess one needs a dedicated .and() method on the same level as .or().

Rails: Why “collection=” doesn't update records with existing id?

大憨熊 提交于 2020-01-15 03:47:07
问题 User can have many posts: class User < ActiveRecord::Base has_many :posts accepts_nested_attributes_for :posts end class Post < ActiveRecord::Base belongs_to :user end Why the following sequence doesn't update the first post? $ rails c > user = User.create(name: 'Misha') => #<User id: 7, name: "Misha", ... > > user.posts << Post.create(description: 'hello') => #<ActiveRecord::Associations::CollectionProxy [#<Post id: 9, description: "hello", user_id: 7, ... >]> > post1 = Post.find(9) > post1