activerecord

Question on Proper Associations in Rails

孤街醉人 提交于 2019-12-11 08:34:44
问题 Take for example this situation. You have 3 models: Poet - Represents the author of a poem Poem - Represents a poem written by a Poet Printing - Represents a printed publication of any sort containing the Poet's Poem. Right off the bat poet and poem are obvious: Poet has_many poems Poem belongs_to poet It becomes confusing for me when dealing with the Printing model. In this case a Printing, in some sense, belongs to both the poet and poem. You could say a poet has_many printings or a poem

ActiveRecord where.not is not working/ weird behavior

我是研究僧i 提交于 2019-12-11 08:34:28
问题 User has_many Plans. I'm trying to find the IDs of all Users that do NOT have a Plan with status of "canceled". Would love to know what's explaining the behavior below. For context, what should be returned is this: User.select { |u| u.plans.select { |p| p.status != "canceled" }.count > 0 }.map(&:id) # => [27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 41, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 61, 62, 63] Here's what I'm getting: # statement 1 User.joins(:plans)

How to enable ActiveRecord to support CJK query?

对着背影说爱祢 提交于 2019-12-11 08:33:30
问题 After rails console , I can execute the following query: 1.9.3-p551 :001 > ActivityObject.where(:title => "kiketurpis integer aliquet") And I got a unique answer existed in database. But if I enter: 1.9.3-p551 :002 > ActivityObject.where(:title => '第一个纵纹') (In double quotes there is a Chinese string.) I got all records in table activity_objects , which means I can not use Chinese string in predicate where . Also, I can directly query this record using the Chinese string under psql : vish

how can I get a DISTINCT list of users who have commented (polymorphic association) on a post?

谁说我不能喝 提交于 2019-12-11 08:25:46
问题 User has_many comments. Comment are in the comments table (polymorphic). User.where("comments.commentable_type = ? AND comments.commentable_id = ?", "Post", post.id).uniq I get: Post Load (0.4ms) SELECT `posts`.* FROM `posts` LIMIT 1 TypeError: Cannot visit Arel::Nodes::Distinct from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:25:in `rescue in visit' from /Users/.rvm/gems/ruby-1.9.3-p125@myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit'

rails sum multiple fields

☆樱花仙子☆ 提交于 2019-12-11 08:23:59
问题 I'm trying to do this supposingly simple operation in rails: self.timesheets.select("sum(total) as total, sum(quantity) as quantity").first where self is a project When I do it in console mode, it works but it renders me 3 columns: [#<Timesheet id: nil, quantity: 120.1, total: 6245.2>] When I run it in the app, I get this error message: PG::GroupingError - ERROR: column "timesheets.id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ..." WHERE "timesheets".

Rails: How to make has_many :through association work with Single Table Inheritance

假如想象 提交于 2019-12-11 08:14:51
问题 So in my current project I have an Article model that can have different kinds of Transactions. It has one main Transaction, but under certain circumstances it can have multiple sub-transactions. Until now I set it up like this: class Article < ActiveRecord::Base has_one :transaction, inverse_of: :article has_many :partial_transactions, through: :transaction, source_type: 'MultipleFixedPriceTransaction', source: 'PartialFixedPriceTransaction', inverse_of: :articles end class Transaction <

CodeIgniter Active Record 'like' ignoring 'where'

微笑、不失礼 提交于 2019-12-11 08:11:56
问题 riting a very simple search using like and have an option to omit options, however I find the like statement is making query ignore the where statement $this->db->like('LOWER(location) OR LOWER(name)', strtolower($term)); $this->db->where('stage', 1); $this->db->order_by("name", "asc"); $query = $this->db->get($this->user_table); return $query->result(); Example of what the above produces with $term = "dublin"; SELECT * FROM (`users`) WHERE `stage` = 1 AND LOWER(location) OR LOWER(name) LIKE

Rails association with delegate methods. possible bug with AR?

北城余情 提交于 2019-12-11 07:57:50
问题 Isolated this question into it's own rails app: and added a git repo as an example Module: module SeoMeta def self.included(base) base.extend(ClassMethods) base.send :include, InstanceMethods end module ClassMethods def is_seo_meta has_one :meta, class_name: SeoMetum, as: :metumable, dependent: :destroy, autosave: true delegate :browser_title, :meta_description, :meta_author, :meta_keywords, :browser_title=, :meta_keywords=, :meta_description=, :meta_author=, to: :meta after_save :save_meta

How would you model articles with references and citations in rails & ActiveRecord?

岁酱吖の 提交于 2019-12-11 07:46:12
问题 An article has many articles that it refers to and many other articles can refer to it. Sometimes an article can refer to an article that also refers to it. 回答1: I'd do it like this: class Article < ActiveRecord::Base # mentions in other articles has_many :references, :foreign_key => 'referred_article_id' # articles that refer to it has_many :referrers, :through => :references, :foreign_key => 'referred_article_id' # articles it refers to has_many :referred_articles, :through => :references,

Is it possible to conditionally allow nested attributes?

大憨熊 提交于 2019-12-11 07:41:43
问题 I have one big form, which ends up creating a User account, and completing part of it's profile. So, User belongs_to Profile . The thing is, I'd like to set the profile to be filled as nested attributes, but only for this form. All other forms for the User model should not allow sending nested attributes for the profile. Is this possible? How? 回答1: You can easily conditionally reject nested attributes based on the contents of the attributes by passing a :reject_if block to accepts_nested