activerecord

Rails :HOw to order a table by associated model

风流意气都作罢 提交于 2019-12-13 06:43:23
问题 I think the problem is trivial. I have two models: User and Betting. User has_many :bettings Betting belongs_to :user I just want to get the users ordered by who made more bettings. 回答1: Are you on Rails2 or Rails3? On Rails3, you can use the Ruby sort method and something like: User.includes(:bettings).sort{|x,y| x.bettings.size <=> y.bettings.size} Of course, in this case the sorting occurs after the SQL request, which is not optimum if you have large tables… I'm trying to figure how to do

change validation error from active record error view page to clean flash message

做~自己de王妃 提交于 2019-12-13 06:13:47
问题 I found the following code at How do I validate members of an array field? . # Validates the values of an Enumerable with other validators. # Generates error messages that include the index and value of # invalid elements. # # Example: # # validates :values, enum: { presence: true, inclusion: { in: %w{ big small } } } # class EnumValidator < ActiveModel::EachValidator def initialize(options) super @validators = options.map do |(key, args)| create_validator(key, args) end end def validate_each

Derived Activerecord Ruby Class

别说谁变了你拦得住时间么 提交于 2019-12-13 06:13:24
问题 I am totally new to ruby. I am trying to make a RESTful service for task tracking application. I researched and found Sinatra better for the job than rails. So I am using Sinatra and ActiveRecord. I am following Up and Running With Sinatra and ActiveRecord. I will be creating the client application in .NET using Restsharp. But this is all about server side. This is the migration I have created class CreateTasksPeopleDocumentsAndComments < ActiveRecord::Migration def self.up create_table

Difficulty with ActiveRecord query that does percentage calculation

。_饼干妹妹 提交于 2019-12-13 06:08:33
问题 I have an ActiveRecord schema, PurchaseSummary, that I inherited and THAT I CAN NOT CHANGE that looks like this: purchase_summaries date date_of_purchase integer number_of_purchases boolean coupon_used example, for November 1st, there were 800 purchases, 500 without a coupon, 300 with a coupon, and on Nov 2nd, 600 purchases without a coupon, and 100 with, so the rows in the database look like this: 2011-11-01, 500, false 2011-11-01, 300, true 2011-11-02, 600, false 2011-11-02, 100, true In

Object with BigDecimals returns empty strings on to_s

倾然丶 夕夏残阳落幕 提交于 2019-12-13 06:06:45
问题 I have a location table I use to store geo coordinates: class Location < ActiveRecord::Base # Location has columns/attributes # BigDecimal latitude # BigDecimal longitude (...) def to_s @latitude.to_s << ', ' << @longitude.to_s end end However, when I call to_s on a Location, the BigDecimal inside converts to an empty string. ruby > l => #<Location id: 1, latitude: #<BigDecimal:b03edcc,'0.4713577E2',12(12)>, longitude: #<BigDecimal:b03ecb4,'-0.7412786E2',12(12)>, created_at: "2011-08-06 03:41

Building a nested attribute from multiple parent objects

自古美人都是妖i 提交于 2019-12-13 05:42:06
问题 I'm currently trying to use link_to with a helper method that creates links for creating items dynamically, rather than using the new_object_path methods. Rails 3 allows for polymorphic URLs, which fl00r helped me identify and use in the form I was originally searching for. However, while using Rails' polymorphic_url method as shown here does pass the argument in the http request, the related form field on the returned page isn't filled in, and isn't getting set. I'm trying to build an order

Case insensitive active record query in rails-4

橙三吉。 提交于 2019-12-13 05:41:03
问题 Currently I am working on a project in rails 4 in which I have a user-page at example.com/username but it only finds the record if i use username in proper case, how can I perform case insensitive search in active record rails. Check out my code below @user = User.find_by_username(params[:username].downcase) 回答1: you will have to use something similar to where("username ILIKE ?") . the ILIKE or similar syntax is dependant on your database though. 回答2: Another option could be: def self.search

Can't delete record through Rails

℡╲_俬逩灬. 提交于 2019-12-13 05:32:31
问题 I have a table, say Persons and if I do person = Person.find(:first) person.delete I get NoMethodError: undefined method `to_sym' for nil:NilClass from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activesupport-3.1.1/lib/active_support/whiny_nil.rb:48:in `method_missing' from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/base.rb:1403:in `block in expand_hash_conditions_for_aggregates' from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active

Implementation of blocked dates for a user-event ORM model

允我心安 提交于 2019-12-13 05:27:10
问题 In continuation of Find entry where value does not intersect with other value I have an application (Ruby on Rails, ActiveRecord, Postgresql) that uses a user table, date table and an event table in order to track users and their events as well as dates on which these events take place. Event has many dates, and user has the ability to sign into events via a different table. The feature I am working on here is for users to be able to block certain dates, so that I can later find 1) users that

Cannot save record to database RAILS nested forms

假如想象 提交于 2019-12-13 05:19:29
问题 I cannot seem to save my record with nested forms. This is what pry says: pry(#<VenuesController>)> @venue.save (0.3ms) begin transaction Tag Exists (0.2ms) SELECT 1 AS one FROM "tags" WHERE ("tags"."name" = 'All ' AND "tags"."id" != 2) LIMIT 1 Tag Exists (0.1ms) SELECT 1 AS one FROM "tags" WHERE "tags"."name" = '' LIMIT 1 (0.1ms) rollback transaction => false I thought I followed everything correctly. This is my nested form for tags Tags: <%= f.collection_check_boxes :tag_ids, Tag.all, :id,