activerecord

Using .average with .pluck in ruby on rails app?

血红的双手。 提交于 2019-12-25 09:03:18
问题 In my app I want to see the average paper use weight for my users which registers to the Heavy class. The user model has_many :papers and the paper model belongs_to :user here is what I got so far: @heavy_users_testing = User.where(industry_type: 'Heavy').joins(:papers).where("papers.paper_type = 'Officepaper'").pluck(:paper_weight) I'm not sure where to but the active record .average to get the average Officepaper weight for users in the Heavy category? Can someone advise me please? 回答1: You

Adding more than one string to a controller find condition?

别来无恙 提交于 2019-12-25 09:01:14
问题 I have the following code in one of my Rails (3.1) controllers: @count = Call.where(:destination => current_user.destination_id).count @count_day = Call.where('created_at > ?', 1.days.ago).count @count_month = Call.where('created_at > ?', 30.days.ago).count They are all working as expected, but I am trying to somehow merge two of them together so it shows the count of calls created in the last 1 day, but only for calls with a destination that matches the current users destination_id value. I

Rails 3 - How can you sort an AR query result by doing math on attributes?

為{幸葍}努か 提交于 2019-12-25 08:58:32
问题 I'm building a controller/view that provides a wide selection of player rankings (e.g. "Top 10 Leader Boards".) Using this model: class Player < ActiveRecord::Base attr_accessible :name, :games_played, :games_lost, :games_won, games_exited, :total_kills, :total_deaths, :total_points, :total_coins end In my controller I have some obvious query results to pass to my view to populate player ranking lists: @top_winners = Player.order("games_won DESC").limit(10) @top_assassins = Player.order(

How can I display all tags from all users for a model instance?

▼魔方 西西 提交于 2019-12-25 08:39:38
问题 I want to get all the tags from all the users in my app for a brand whose ID is 37. The following works but only gets the tags from one of the 2 users currently in the app: <%= BrandUser.last.brand.tags.join(", ") %> The following is my attempt but doesn't work: <%= BrandUser.where(:brand_id => 37).each {|brand| p brand.tags} %> Brand has_many tags, has_many brand_users and has_many users through brand_users User has_many :brand_users and has_many :brands, :through => :brand_users BrandUser

Rails 3 Override Destroy without Canceling callbacks, and triggering a ROLLBACK

佐手、 提交于 2019-12-25 08:35:41
问题 My desired behavior is that when destroy is called on an instance, that instance will not actually be destroyed, but it will just be marked as having been destroyed. This needs to percolate up for any model associations. In the models that I don't want to actually destroy, but just mark them as deleted I have a deactivated field. From what I can tell the < v3.0.0 way of doing this was to override destroy_without_callbacks (That's the way ActsAsParanoid does it), however that method no longer

Identify Resource that Does Not Have a Payment Association with SQL query

断了今生、忘了曾经 提交于 2019-12-25 08:35:18
问题 I have an index page where I want to both show featured and standard boards. A featured board has has_one payment, a standard board does not. class Payment < ApplicationRecord belongs_to :board end class Board < ApplicationRecord has_one :payment end So I can identify a featured board by joining the payments and therefor removing the standard boards. Board.joins(:payment).where(category: category, confirmed: true) Now I want to get the standard listings by doing: Board.where(category:

Weird rounding issue

血红的双手。 提交于 2019-12-25 08:22:02
问题 Something very weird is happening with decimals and floating numbers and I can't understand why or where (ruby/rails/postgresql). Given a purchases table with a decimal column - total : p1 = Purchase.where(total: 5.99).first_or_create p2 = Purchase.where(total: 5.99).first_or_create [p1.id, p2.id] # => [1, 2] p3 = Purchase.where(total: 5.99.to_d).first_or_create p4 = Purchase.where(total: 5.99.to_d).first_or_create [p3.id, p4.id] # => [1, 1] Both Ruby and postgresql have no problem

Weird looping behaviour - what's happening here?

孤人 提交于 2019-12-25 08:10:38
问题 Gotten myself all confused with what I think is a double loop and don't know how to fix it. Can anyone save me from going completely mad please? This works in my show view: <% @releases_tracks_temp.each do |releases_track| %> <tr> <td><%= releases_track.track.id %> / <%= releases_track.position %></td> <td><%= releases_track.track.name %> <%= releases_track.track.artists.map { |a| a.name}.join (", ") %></td> <td><%= releases_track.track.isrc %></td> <td><%#= link_to image_tag("icons/delete

Automatically remove a record after a few minutes

穿精又带淫゛_ 提交于 2019-12-25 07:53:42
问题 I am going to use timeout as advised in my first question. So if a person views a post, he will be recorded into the list of people who are viewing the same post, and this record will be expired after a certain period of time (e.g. 5 minutes). Someone advised me to use cache to achieve this, but I think I will try to do it with Rails associations like follows. post has_many readers And in the PostsController#Show @post.readers.create(user: current_user) and I need to make this record to be

How can I get the datatype of an attribute from a Rails model?

空扰寡人 提交于 2019-12-25 07:29:44
问题 I am using Postgres in a Rails project and I have discovered that I need to change all of my varchar datatypes to citext . Rather than do this by hand, I want to just create a migration that loops through all of the models and their attributes and converts them as necessary. Most of these models are empty, so it's not a matter of instantiating them. I need to find out if ActiveRecord "knows" what the datatype of its corresponding database column is. 回答1: @model.column_for_attribute('title')