activerecord

DISTINCT ON query w/ ORDER BY max value of a column

て烟熏妆下的殇ゞ 提交于 2019-12-23 00:52:39
问题 I've been tasked with converting a Rails app from MySQL to Postgres asap and ran into a small issue. The active record query: current_user.profile_visits.limit(6).order("created_at DESC").where("created_at > ? AND visitor_id <> ?", 2.months.ago, current_user.id).distinct Produces the SQL: SELECT visitor_id, MAX(created_at) as created_at, distinct on (visitor_id) * FROM "profile_visits" WHERE "profile_visits"."social_user_id" = 21 AND (created_at > '2015-02-01 17:17:01.826897' AND visitor_id <

association data in rails model scope

谁都会走 提交于 2019-12-22 23:47:22
问题 I have a model named Post (blog post) and a model named Category . Each post belongs_to a category. Each category has an attribute named retainer that specifies the amount of time before a post "expires", so for example movies_category.retainer = 30.days What I'm trying to do is create a scope for Post that finds all of the posts which are "expired". So for example, assuming I were to hardcode the value of 30.days and it were to apply to all categories (therefore all posts), the scope would

Rails ActiveRecord query using inner join

一个人想着一个人 提交于 2019-12-22 22:18:32
问题 I have two tables that I'm attempting to do an inner join with. One is a users table where the primary key is id . Another table is bars where user_id is a foreign key. bars also has a column called foo_id where food_id is a foreign key to the foos table. I am trying to put together an ActiveRecord query where I can select all users that were created on or before N days ago and do not have any foos where bars.foo_id equal to a particular id. I tried doing something like this: users = User

Rails ActiveRecord query using inner join

老子叫甜甜 提交于 2019-12-22 22:18:28
问题 I have two tables that I'm attempting to do an inner join with. One is a users table where the primary key is id . Another table is bars where user_id is a foreign key. bars also has a column called foo_id where food_id is a foreign key to the foos table. I am trying to put together an ActiveRecord query where I can select all users that were created on or before N days ago and do not have any foos where bars.foo_id equal to a particular id. I tried doing something like this: users = User

Rails getting undefined method for existing field in database

萝らか妹 提交于 2019-12-22 18:59:06
问题 So I have a field in the question table, called is_public..I migrated my database, restarted it and now when I get this error everytime I call is_public for a record in the question records. undefined method `is_public' for #<Question:0x7e9aa780> Any ideas to why this is happening ? 回答1: Are you using attr_accessible? If so, add :is_public to the list. 来源: https://stackoverflow.com/questions/5032922/rails-getting-undefined-method-for-existing-field-in-database

Order an ActiveRecord relation object

强颜欢笑 提交于 2019-12-22 18:38:30
问题 I have an ActiveRecord Object called contact . It has an relation called profiles . These profiles have a url property. The profiles should be ordered by url in alphabetical order. I've tried sort_by as well as order but I get this error: contact.profiles.sort_by! { |profile| profile.url } undefined method `sort_by!' for #<Profile::ActiveRecord_Associations_CollectionProxy:0x00000105d6d430> What's the best way to do this? I'm using Rails v4.1.0. 回答1: Use order query method for sorting the

Retrieving sql queries from Active-record queries in Rails 3

情到浓时终转凉″ 提交于 2019-12-22 18:24:40
问题 How do I trace which SQL query did my Activerecord methods generated (eg find, where). 回答1: You can debug ActiveRecord queries from a console. Hit rails console and enter: ActiveRecord::Base.logger = Logger.new(STDOUT) 回答2: I assume you're using Rails 3.0.x, you can do that by configuring your active record. Put this in config/environments/development.rb # Log ActiveRecord ActiveRecord::Base.logger = Logger.new(STDOUT) if defined? Rails::Console Now, every query is explained in console. 回答3:

Can I use ActiveRecord relationships with fields from an Hstore?

对着背影说爱祢 提交于 2019-12-22 18:22:01
问题 Can I tie a model to another through active record belongs_to using a field from an hstore hash? I'll elaborate: I have a User model that gets subclassed via STI on one of its fields to many different other User models based on permissions: class User < ActiveRecord::Base self.inheritance_column = :role #other definitions and validations end Here's one such submodel, the nightclub_boss model, meant for administrative users for my app: class NightclubBoss < User belongs_to :nightclub #the boss

Record Expiration

喜夏-厌秋 提交于 2019-12-22 18:14:23
问题 My Rails app allows administrators to give other players infractions, which are stored in a database table. These infractions have point values that add up to give the player a points value. However, these records will contain an expiry time stamp. Is there any way of automatically deleting the infraction record when the expiry date has been reached? 回答1: How about using a default scope that filters out expired records? Something like class Infraction < ActiveRecord::Base default_scope -> {

Mysql::Error: Duplicate entry

≯℡__Kan透↙ 提交于 2019-12-22 17:51:25
问题 I have a model class Gift < ActiveRecord::Base validates_uniqueness_of :giver_id, :scope => :account_id end add_index(:gifts, [:account_id, :giver_id], :uniq => true) Action def create @gift= Gift.new(params[:gift]) if @gift.save ... else ... end end In the "production" mode, I sometimes get an error ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry '122394471958-50301499' for key 'index_gifts_on_account_id_and_user_id' What the problem? 回答1: It looks like the gifts table has an