rails-activerecord

Rails: ActiveRecord where vs merge

心不动则不痛 提交于 2019-12-23 06:58:13
问题 First, I am getting the review statuses between particular dates. date_range = Date.parse(@from_date).beginning_of_day..Date.parse(@to_date).end_of_day @review_statuses = ReviewStatus.where(updated_at: date_range) Next, I need to apply an 'AND' condition. @review_cycle = params[:review_cycle] if @review_cycle.present? @review_statuses = @review_statuses.merge( ReviewStatus.where(evidence_cycle: @review_cycle) .or(ReviewStatus.where(roc_cycle: @review_cycle))) end Now for the below should I

Rails Active Record Associations for model which may map to different model types

妖精的绣舞 提交于 2019-12-23 05:19:38
问题 I'm building an RoR application which has a User and Product classes. A user many have many photos as may a product, but each must also have a single profile_picture . Users: class User < ActiveRecord::Base has_many :pictures end Products: class Product < ActiveRecord::Base has_many :pictures end I'm struggling to define the pictures model which is currently: class Picture < ActiveRecord::Base has_one :user has_one :product end The schema for pictures is as follows (timestamps omitted for

Create both associated models from the form of the “belongs_to” one (rails)

▼魔方 西西 提交于 2019-12-23 04:39:15
问题 With the hope that my consecutive questions are not a problem for anyone, I want to ask for your assistance on how to achieve the creation of two models from one form. The two associated models are class Employee < ActiveRecord::Base attr_accessible :name belongs_to :company attr_accessible :company_id end class Company < ActiveRecord::Base attr_accessible :name has_many :employees end and what I want is to create a company when I am creating an employee and the input company doesn't already

How to integrate :missed days with :committed days in habits.rb?

依然范特西╮ 提交于 2019-12-23 02:58:12
问题 How can we integrate t.integer :missed with t.text :committed so that when a User checks off he :missed 3 :committed days in a :level he has to restart the :level ? for each :missed day he checks off, an additional :committed day is added back into the :level so that he must make it up before advancing? Each habit has 5 levels before "Mastery" is achieved! class Habit < ActiveRecord::Base belongs_to :user before_save :set_level acts_as_taggable serialize :committed, Array def self.comitted

Only allow users to create one review per movie

℡╲_俬逩灬. 提交于 2019-12-23 01:21:06
问题 I have my app setup where users can write reviews for a movie. What I'd like to do is limit the user to create only one review per movie. I've managed to accomplish this in my reviews controller as so: class ReviewsController < ApplicationController before_action :has_reviewed, only [:new] .... def has_reviewed? if Review.where(user_id: current_user.id, movie_id: @movie.id).any? redirect_to movie_reviews_path flash[:notice] = "You've already written a review for this movie." end end end Where

How to select the latest record of each hour in a day

妖精的绣舞 提交于 2019-12-22 11:19:13
问题 I want to select the latest record of each hour in a given date, based on the datetime column 'reading_on'. I have executed the below query hourly_max = InverterReading .where("DATE(reading_on) = ? AND imei = ?", Date.today, "770000000000126") .group("HOUR(reading_on)") .having("max(HOUR(reading_on))") hourly_max.group_by(&:id).each { |k,v| puts v.last.reading_on } In the above query I am not getting the required result. What is the proper way to select the latest record of each hour in a day

Applying aggregate function SUM together with GROUP BY

允我心安 提交于 2019-12-22 08:55:11
问题 What is the best way to execute this query in Active Record / Ruby on Rails SELECT sum(amount) as total_amount FROM payments GROUP BY person_id, product_id I can't seem to chain sum with group method. Do I have to resort to find_by_sql here? 回答1: Something like: Payment.group(:person_id).group(:product_id).sum(:amount) should give you a hash whose keys are [person_id, product_id] arrays and whose values are the sums. You probably don't want to: Payment.group('person_id, product_id').sum(

How to group/select JSON type column (PG::UndefinedFunction: ERROR: could not identify an equality operator for type json)

不羁的心 提交于 2019-12-22 08:48:53
问题 I wanna do <MODEL>.select("brief_content").group("brief_content") Here's the table scheme , :id => :integer, :content => :json, :brief_content => :json But I got the errors, How can I do, Thanks companyAlarmLog Load (0.9ms) SELECT company_alarm_logs.id, company_alarm_logs.name, company_alarm_logs.utc_time, company_alarm_logs.company_alarm_test_id, company_alarm_logs.brief_content, brief_content FROM "company_alarm_logs" GROUP BY brief_content ORDER BY utc_time E, [2014-06-24T09:40:39.069988

Rails 3 ActiveModel: cannot include ActiveModel::Model directly

旧时模样 提交于 2019-12-22 05:11:52
问题 In my Rails 3.2.11 and "development" environment when I try to have an active model: class DisponibilityApi include ActiveModel::Model attr_accessor :start_time, :end_time validates :start_time, :end_time, :presence => true end I have an error: NameError: uninitialized constant ActiveModel::Model But when I include it manually: class DisponibilityApi extend ActiveModel::Naming extend ActiveModel::Translation include ActiveModel::Validations include ActiveModel::Conversion attr_accessor :start

rails 3/postgres - how long is a string if you don't apply :limit in schema

时光怂恿深爱的人放手 提交于 2019-12-22 02:05:40
问题 My googlefu must be weak because I cannot find anything to tell me the default limit of a string column in my Rails app (hosted at Heroku, using PostgreSQL as the database). Any help would be appreciated! 回答1: ActiveRecord uses varchar(255) (or character varying (255) to be pedantic) if you don't specify a specific limit. You can always hop into PostgreSQL with psql and say \d your_table to get the table as PostgreSQL sees it. I don't think the default is specified anywhere but it is right