activerecord

Rails 2.3.x - how does this ActiveRecord scope work?

非 Y 不嫁゛ 提交于 2019-12-23 14:23:13
问题 There's a named_scope in a project I'm working on that looks like the following: # default product scope only lists available and non-deleted products ::Product.named_scope :active, lambda { |*args| Product.not_deleted.available(args.first).scope(:find) } The initial named_scope makes sense. The confusing part here is how .scope(:find) works. This is clearly calling another named scope (not_deleted), and applying .scope(:find) afterwards. What/how does .scope(:find) work here? 回答1: A quick

Rails 2.3.x - how does this ActiveRecord scope work?

假如想象 提交于 2019-12-23 14:23:03
问题 There's a named_scope in a project I'm working on that looks like the following: # default product scope only lists available and non-deleted products ::Product.named_scope :active, lambda { |*args| Product.not_deleted.available(args.first).scope(:find) } The initial named_scope makes sense. The confusing part here is how .scope(:find) works. This is clearly calling another named scope (not_deleted), and applying .scope(:find) afterwards. What/how does .scope(:find) work here? 回答1: A quick

How can I find records from today, yesterday and so on with Ruby on Rails?

匆匆过客 提交于 2019-12-23 14:11:09
问题 I want to find all records, say Posts, created today with Ruby on Rails, then all Posts created yesterday, and so on… how should I do? Thank you, Kevin 回答1: Try this: #Today Posts.find(:all, conditions: { :created_at => Date.today...Date.today + 1 }) #Yesterday Posts.find(:all, conditions: { :created_at => Date.today - 1...Date.today }) Or this (preferable, in my opinion): #Today Posts.find(:all, conditions: ["DATE(created_at) = ?", Date.today] ) #Yesterday Posts.find(:all, conditions: ["DATE

Filter by count of children using Rails 3

允我心安 提交于 2019-12-23 13:09:36
问题 I'd like to select posts that have one or more comments using Rails 3 and a single query. I'm trying something like this: Post.includes(:comments).where('count(comments.id)>0') However I get this error: ActiveRecord::StatementInvalid: PGError: ERROR: aggregates not allowed in WHERE clause I've googled this and similar approaches, group by, etc with no luck. Any help will be appreciated. 回答1: I'm sure you may have figured this out by now, but I believe what you're looking for is the having(

How and where to handle the updating process of associated records?

江枫思渺然 提交于 2019-12-23 12:55:40
问题 I am using Ruby on Rails 4 and I would like to properly handle the updating and creating process of associated records through the update_attributes method. That is, I have the following: # Model class Article < ActiveRecord::Base has_many :categories accepts_nested_attributes_for :categories end # Controller class ArticlesController < ApplicationController def update @article = Article.find(params[:id]) @article.update_attributes(update_params) ... end private def update_params params

how to hook for destroy of a model that belongs to another model?

巧了我就是萌 提交于 2019-12-23 12:51:48
问题 I have a User model which has_many experiments: class User < ActiveRecord::Base has_many :experiments, :dependent => :destroy and Experiment model: class Experiment < ActiveRecord::Base belongs_to :user has_attached_file :thumbnail I want to hook for destroy moment at the Experiment model after the owner User get destroyed. (ex user cancel his account) I need to do so to delete the attachment image of the Experiment model, which is stored at amazon. like experiment.thumbnail.destroy What is

Adding a column to a model at runtime (without additional tables) in rails

走远了吗. 提交于 2019-12-23 12:46:00
问题 I'm trying to give admins of my web application the ability to add some new fields to a model. The model is called Artwork and i would like to add, for instante, a test_column column at runtime. I'm just teting, so i added a simple link to do it, it will be of course parametric. I managed to do it through migrations: def test_migration_create Artwork.add_column :test_column, :integer flash[:notice] = "Added Column test_column to artworks" redirect_to :action => 'index' end def test_migration

Globalize3 order records by translated attributes and taking fallbacks into consideration

随声附和 提交于 2019-12-23 12:35:34
问题 Im having troubles with awesome Globalize3 gem. For now I`m having two languages :en and :ru. And :ru falls back to :en like this #/config/initializers/globalize.rb Globalize.fallbacks = {:ru => [:ru, :en]} In my controller I am trying to sort the whole collection of translated records either by name translations or by translations fallback values. But with_translations() does not seem to give me such opportunity! Country.with_translations(:ru).order('country_translations.name ASC') #this

joins() and where() request on association with custom table name

拥有回忆 提交于 2019-12-23 12:32:40
问题 I have two models Album.rb class Album < ActiveRecord::Base has_many :tracks self.table_name = 'prefix_album' end Track.rb class Track < ActiveRecord::Base belongs_to :album self.table_name = 'prefix_track' end Now, because reasons, the table names are prefixed, so I have prefix_album and prefix_track tables in my database. For basic use, it works fine. Now the problem with the following query : Album.joins(:tracks).where(tracks: { id: [10, 15] }) Results in the following SQL : SELECT * FROM

Postgres window function column with Rails

亡梦爱人 提交于 2019-12-23 12:24:41
问题 I'd like to use the rank() function PostgreSQL on one of my columns. Character.select("*, rank() OVER (ORDER BY points DESC)") But since I don't have a rank column in my table rails doesn't include it with the query. What would be the correct way to get the rank included in my ActiveRecord object? 回答1: try this: Character.find_by_sql("SELECT *, rank() OVER (ORDER BY points DESC) FROM characters") it should return you Character objects with a rank attribute, as documented here . However, this