activerecord

ruby on rails 3 reload using cached data

风流意气都作罢 提交于 2019-12-22 10:58:07
问题 Using the activerecord reload command in my app seems to be using cached data when called. I can replicate within the debugger by doing the following. u = User.find(1) u.first_name #outputs bob # manually change first_name for record 1 to jim with PGadmin or with rails console u.reload u.valid? #outputs true u.first_name #outputs bob #if i do this again u = User.find(1) #old data again u.first_name #outputs bob #if i load data this way u = User.where('id = 1').first #new data u.first_name

How do I sum up records within Rails 3?

一曲冷凌霜 提交于 2019-12-22 10:44:45
问题 I have an ItemsSold model which has the total number of magazines, books, videos, greeting_cards, pens sold for a single day. How do I elegantly return an array with the weekly sold totals for each item for the last arbitrary number of weeks? I have a model file: #items_sold.rb class ItemsSold < ActiveRecord::Base attr_accessible :magazines, :books, :videos, :greeting_cards, :pens, :sold_date end My table is defined as follows: t.integer :magazines t.integer :books t.integer :videos t.integer

Rails 3 with composed_of model and validation

让人想犯罪 __ 提交于 2019-12-22 10:11:32
问题 I have this domain model: class Person < ActiveRecord::Base composed_of :address, mapping: [%w(address_street street), %w(address_city city), %w(address_zip_code zip_code), %w(address_country country)] validates :name, presence: true, length: { maximum: 50 } validates :surname, presence: true, length: { maximum: 50 } validates_associated :address end class Address include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming attr_reader :street, :city, :zip_code,

How do I write an activerecord query that only looks at the time component of a datetime field?

▼魔方 西西 提交于 2019-12-22 09:55:33
问题 Is it possible to do an activerecord query that only looks at the time component of a datetime field? e.g. Battle.where('start_time < ? and start_time > ?','12:00','06:00') to find all battles that were started between 6am and 12pm regardless of the day they occurred? In this example, start_time is defined as a datetime. 回答1: The only way to do this is using a SQL function, if you're on MySQL you could do it like this: Battle.where( 'HOUR( start_time ) >= ? AND HOUR( start_time ) <= ?', 12, 6

Why does this 'validate' method raise an ArgumentError?

送分小仙女□ 提交于 2019-12-22 09:53:39
问题 Folks, I can't get validates_with in my (helloworld-y) rails app to work. Read through "callbacks and validators" section of the original RoR guides site and searched stackoverflow, found nothing. Here's the stripped-down version of code I got after removing everything that can fail. class BareBonesValidator < ActiveModel::Validator def validate # irrelevant logic. whatever i put here raises the same error - even no logic at all end end class Unvalidable < ActiveRecord::Base validates_with

Sort by specific ids in ActiveRecord

最后都变了- 提交于 2019-12-22 09:43:28
问题 I have inherited another programmer's Rails3 project, and I'm fairly new to rails overall. He's got a query that appears to sort by specific id's. Can somebody explain how this resolves in actual SQL? I think this code is killing the db and subsequently rails. I've tried to output it in the logger but can't seem to get the actual SQL to output even with config set to :debug. Searching heavily here (on SO) didn't turn up a clear explanation of how this query looks. The code looks like: options

Complex Database Queries in yii2 with Active Record

℡╲_俬逩灬. 提交于 2019-12-22 09:15:15
问题 TL;DR I have a query that works in RAW SQL but i have had little success recreating it with query builder or active record. I am working on a web application based off of the yii2 advanced application template. I have written a database query and implemented it with findbysql() returning the correct records but am having trouble translating this into active record. I originally wanted to allow the user to modify (filter) the results by means of a search form(user & date), however i have since

Active Record LIMIT within GROUP_BY

二次信任 提交于 2019-12-22 08:46:42
问题 SCENARIO I have a table full of posts with a users table. I want to be able to fetch all the posts and group them by users but I want to set a limit of say 10 per user. class Post < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_many :posts end # I thought this might work but it just grabs the first 10 posts and groups them Post.find(:all, :limit=>10).group_by(&:user) Any thoughts? Do I have to write custom SQL for or can Active Record do this? 回答1: Something like

Is there a way to rename ActiveRecord model columns?

▼魔方 西西 提交于 2019-12-22 08:37:01
问题 I am thinking about choosing rails' ActiveRecord to have access to a legacy database. It's names are really confusing, so it wouldn't be a good idea to use its column names in the model. Setting table name is really easy. But do I have a way to rename column name, only in the model? Convention over configuration is great, but in this case I can't change legacy database names. Using alias_attribute from ActiveSupport doesn't solve my problem, since the object still shows the legacy column

Select, group and sum results from database

安稳与你 提交于 2019-12-22 07:21:04
问题 I have a database with some fields I'd like to sum. But that's not the big problem, I want to group those fields by the month they were created. ActiveRecord automaticaly created a field named "created_at". So my question; how can I group the result by month, then sum the fields for each month? Updated with code @hours = Hour.all(:conditions => "user_id = "+ @user.id.to_s, :group => "strftime('%m', created_at)", :order => 'created_at DESC') This is the code I have now. Managed to group by