activerecord

Rails 4.x how to query boolean value with activerecord?

梦想与她 提交于 2019-12-10 17:52:48
问题 I a boolean "guessed" on my model "perks" and I am trying to get all customers that have perks where guessed is true. My first guess was this: @customer = Customer.includes(:perks).where('perks.guessed = true') But that gives me an SQL-exception: "no such column: true" A bit of googling gave me this thread: Rails - how to search based on a boolean field? (MySQL error) So I tried: @customer = Customer.includes(:perks).where('perks.guessed = 1') No luck... Also tried: @customer = Customer

What's the difference between design pattern and enterprise design pattern?

亡梦爱人 提交于 2019-12-10 17:47:31
问题 I want to know the difference between design pattern and enterprise design pattern e.g. some books call ActiveRecord an enterprise design pattern, while singleton is a design pattern. 回答1: It probably has the most to do with what book it came from. Singleton was first popularized in "Design Patterns" by the Gang of Four. ActiveRecord was in "Patterns of Enterprise Application Architecture", by Martin Fowler. The Gang of Four described Design Patterns as generally useful object-oriented class

errors.full_messages: attribute name appears twice

心已入冬 提交于 2019-12-10 17:36:20
问题 This has been bugging me for a while. This problem occurs with all of my models, but i'll use one of them, Quiz, as an example. Quiz has the following validations: validates_presence_of :size, :style I'm using I18n, and i have the following set in my translations file: (these are just the standard error messages, but i've included them in my en.yml so that it's easy to see the structure, if i want to override them for any particular model) activerecord: errors: messages: inclusion: "{

RSpec Matchers When Working With ActiveRecord::Relation

泪湿孤枕 提交于 2019-12-10 17:33:58
问题 So here's the method I want to test: def self.by_letter(letter) where("lastname LIKE ?", "#{letter}%").order(:lastname) end Quick question here, what exactly does the percent sign after #{letter} do? Something to do with formatting? Here's part of the spec that tests that method: context 'method "by_letter"' do it 'returns and ordered list by letter' do theon = Contact.create!( firstname: "Theon", lastname: "Greyjoy", email: "tgreyjoy@ironprice.com" ) rob = Contact.create!( firstname: "Rob",

Is it really needed to validate foreign keys?

自闭症网瘾萝莉.ら 提交于 2019-12-10 17:22:30
问题 I am using Ruby on Rails v3.2.2 and, after post my previous question, I would like to know and understand if (or not) to explicitly validate foreign keys related to ActiveRecord::Associations is needed. For example: class CategoryAssociation < ActiveRecord::Base belongs_to :article, :foreign_key => 'article_id' belongs_to :category, :foreign_key => 'category_id' validates :article_id, :presence => true, :numericality => { :only_integer => true } validates :category_id, :presence => true,

In rubyonrails, how to get the associated model class from and ActiveRecord::Relation object?

陌路散爱 提交于 2019-12-10 17:17:31
问题 Suppose I have an model: class Post end posts = Post.where(***) puts posts.class # => ActiveRecord::Relation Then how can I get the model class name through the variable 'posts', maybe some method called model_class_name: puts posts.model_class_name # => Post Thanks :) 回答1: The #klass attribute of ActiveRecord::Relation returns the model class upon which the relation was built: arel = User.where(name: "fred") arel.klass # User To get the class's name: arel.klass.name Tested in ActiveRecord 4

Can I create an activerecord association through an activerecord relation (when using the ancestry gem)?

心已入冬 提交于 2019-12-10 17:17:14
问题 I am using the ancestry gem in my rails project to create a hierarchy of groups. A group can belong to a parent group and can have many children groups. Each group can have many users who belong to a group. The model looks like this: class Group < ActiveRecord::Base has_ancestry has_many :users end I would love to be able to get all users for the descendants of a group, something like this: class Group < ActiveRecord::Base has_ancestry has_many :users has_many :descendants_users, through:

ror - include foreign key at both ends of has_many and belongs_to?

假装没事ソ 提交于 2019-12-10 17:15:28
问题 I'm inheriting code which has: class Graphic < ActiveRecord::Base has_many :comments, :foreign_key => 'asset_id', :conditions => 'asset_type_id = 5', :order => 'created_at', :dependent => :destroy class Comment < ActiveRecord::Base belongs_to :graphic, :foreign_key => :asset_id It seems to me like the has_many should not have the foreign_key (it's referenced in the belongs_to ok I believe) but I am not sure, do you know? i.e. should it be class Graphic < ActiveRecord::Base has_many :comments,

Import SQL file to Rails 4 ActiveRecord db?

て烟熏妆下的殇ゞ 提交于 2019-12-10 17:08:58
问题 I've looked over several other questions on here, and they're vaguely similar, but not exactly what I'm looking for. What I'm trying to do is import/"convert" a *.sql file which contains 8 tables, each of which contain roughly 24 columns. This file is actually fairly flat file, seeing as though the only queries that worked previous had to do with associating a shared :id between tables (so, SELECT * FROM table1, table2 WHERE id = '1' would pull all results, which was fine at the time). I've

Is there any way to use AREL for custom associations?

送分小仙女□ 提交于 2019-12-10 17:08:22
问题 model Post # ActiveRecord associations have tons of options that let # you do just about anything like: has_many :comments has_many :spam_comments, :conditions => ['spammy = ?', true] # In Rails 3, named scopes are ultra-elegant, and let you do things like: scope :with_comments, joins(:comments) end Is there any way to use AREL, or an otherwise leaner syntax, to define custom associations as elegantly as named scopes? update I've decided it's not a good idea to put that sort of detail into an