rails-activerecord

ActiveRecord : Hide column while returning object

倾然丶 夕夏残阳落幕 提交于 2019-12-06 18:39:25
问题 Is there an out-of-the-box way to always hide/remove a column (say, User.password) while returning an ActiveRecord object ? Thanks. 回答1: Using the built-in serialization, you can override the as_json method on your model to pass in additional default options: class User < ActiveRecord::Base # ... def as_json(options = {}) super(options.merge({ except: [:password, :oauth_token] })) end end There are probably better serialization tools out there - if you are looking for more fine-grained

Render failing to render correct template in rescue_from ActiveRecord::Rollback method

心已入冬 提交于 2019-12-06 15:14:45
I'm building the checkout page for an e-commerce site, and I have a fairly long transaction that creates a new User model and a new Order model. I wrapped the creation of these models in a transaction so that if validation for one fails, the other isn't hanging around in the database. Here's the trimmed-down code in my OrdersController: rescue_from ActiveRecord::Rollback, with: :render_new def render_new render action: 'new' end ActiveRecord::Base.transaction do @user = User.new params[:user] unless @user.save raise ActiveRecord::Rollback end //More stuff ... @order = Order.new params[:order]

Sort list of objects according to an array (Rails)

时间秒杀一切 提交于 2019-12-06 14:21:44
I have an array if ids from a table e.g: b = [659, 658, 656, 645, 644, 657, 634, 643, 649, 650, 651, 636, 633, 607, 605, 604, 648, 647, 675, 674, 667, 499] I use it for retrieving a list of objects from tat table: k = Photo.where(id:b) What I get is a list of objects which are NOT sorted in the same order as the array. How can I do that? If ids are unique You can use index_by and values_at : k = Photo.where(id: b).index_by(&:id).values_at(*b) Example : b = [5,3,1] Country.where(id: b) #=> [#<Country id: 1, name: "France">, #<Country id: 3, name: "Ukraine">, #<Country id: 5, name: "Spain">]

Many-to-many connection and Associations

牧云@^-^@ 提交于 2019-12-06 13:17:02
问题 A User can Upload a Clip via the Clips Scaffold , this clip either belongs to a Show and or an Album (Show Controller, Album Controller). A user can have ONLY ONE SHOW MANY ALBUMS What i'm trying to accomplish: A User can Upload Clips , these clips can be attached to either a Show or/and an Album For a more detailed overview take a look at the image below: Problem #1 What i'm not understanding is the associations i must use. My current Models: User Model : has_many :clips, dependent: :destroy

Rails scope filter by date range

人走茶凉 提交于 2019-12-06 12:49:29
问题 There are many questions relate to rails date range problem but mine is a little more complicated. I have two models: house and booking. A House has_many bookings. A Booking has two attributes in date format: check_in and check_out. What I want to achieve: Giving a valid date range, show all houses that are available during this range. In detail: The start date of the range should not be in any booking. The end date of the range should not be in any booking. There should not be any booking

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

折月煮酒 提交于 2019-12-06 04:48:41
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. Below is the table structure SELECT HOUR(a.reading_on) As hr, max(a.id),a.reading_on FROM

rails callbacks not getting executed

半城伤御伤魂 提交于 2019-12-06 04:13:25
问题 For my life I trying to find out why are my callbacks are not getting executed sometimes (you heard it right sometimes as most of time it works out of the box) All I have is parent/child relations between 2 models upon creation of child record all I'm doing in after_create callback is update(accumulate all child amount in parent field to avoid heavy query at run time) the amount field in parent table/ model record Parent model( Payout ) Child model is ( Sales Transaction ) Payout has_many

undefined method `key?' for nil:NilClass

半城伤御伤魂 提交于 2019-12-06 04:03:06
问题 I'm new to Rails and was following Ryan Bate's tutorial on how to make a simple authentication system (http://railscasts.com/episodes/250-authentication-from-scratch?autoplay=true) and I was just going through it but got this error: ` NoMethodError in UsersController#new undefined method `key?' for nil:NilClass Rails.root: C:/Sites/authentication` I don't really know what this means since I'm just a beginner, but these are my files: users controller: class UsersController <

HABTM checkbox in a nested form

眉间皱痕 提交于 2019-12-06 03:56:01
问题 I am trying to implement a HABTM checkbox in a nested form. Currently, I have 3 models. Subject, lesson and groups. The associations are as follows: Each subject has many lessons. Each lesson has and belongs to many groups. Right now, I am trying to implement them all on a single creation and edit form. Such that a lesson is nested in the subject and for each lesson there is a list of group check boxes to implement the HABTM relationship. I am facing trouble implementing the HABTM

Unable to autoload constant ActiveStorage::Blob::Analyzable Error with Rails 5.2, AWS S3, and ActiveStorage

半城伤御伤魂 提交于 2019-12-06 03:10:02
I've been battling this guy for a while and have done all the Googlies on it ( here , here , and many equally-unhelpful others) but to no avail. The official error is this, called on the first line of my create method: Unable to autoload constant ActiveStorage::Blob::Analyzable, expected /Users/lizbayardelle/.rvm/gems/ruby-2.5.0/gems/activestorage-5.2.1/app/models/active_storage/blob/analyzable.rb to define it I'm creating a blog model, which has_one_attached :pdf and one :image , both through ActiveStorage. These relationships are all in the blog model: class Blog < ApplicationRecord belongs