activerecord

ActiveRecord query to return related Documents using RelatedDocuments join table

坚强是说给别人听的谎言 提交于 2019-12-12 02:19:53
问题 In my app, users can collect favorite Documents . I want to return a list of documents most relevant to those they have favorited, using a related_documents join table that has a relevance_score:integer for the relationship between a document and a related document. TLDR; see the update at the bottom for an almost-working query. Without success, I have tried various queries like: some_user.favorited_documents.joins(:related_to_documents) .select("docid_id FROM documents.related_documents")

ActiveRecord calculations on subsets or records

一曲冷凌霜 提交于 2019-12-12 02:15:35
问题 I have a Game model with a points attribute, and I want to compute the sum of the top 20 scores. I can pluck the points and have ruby compute the sum, as in: Game.order('points desc').limit(20).pluck(:points).sum But I am curious as to whether there is a straightforward way to have AR produce an SQL aggregate calculation to accomplish the same. The following naive attempt does not work: Game.sum(:points, order: 'points desc', limit: 20) SELECT SUM(`games`.`points`) FROM `games` Thanks 回答1:

Multiple limit condition in mongodb

江枫思渺然 提交于 2019-12-12 02:06:42
问题 I have a collection in which one of the field is "type". I want to get some values of each type depending upon condition which is same for all the types. Like I want 2 documents for type A, 2 for type B like that. How to do this in a single query? I am using Ruby Active Record. 回答1: Generally what you are describing is a relatively common question around the MongoDB community which we could describe as the "top n results problem". This is when given some input that is likely sorted in some

Rails: why time datatype start at 1/1/2000

会有一股神秘感。 提交于 2019-12-12 01:59:08
问题 I have a column that I only want to store time (hour, minute, second). for example here is one migration: add_column :products, :start_hour, :time When I insert to database start_hour always start with 1/1/2000 for example: 2000-01-01 01:19:00 +0700 . Please explain for me why. 回答1: There is no time-of-day class in Ruby or Rails so ActiveRecord represents time columns with datetime/timestamp classes. If you look inside the database without any of the ActiveRecord, Rails, or Ruby noise in the

Why doesn't this Ruby on Rails code work as I intend it to?

こ雲淡風輕ζ 提交于 2019-12-12 01:57:48
问题 So I attempted to build what I asked about in this question: Fix voting mechanism However, this solution doesn't work. A user can still vote however many times he or she wants. How could I fix this and/or refactor? def create @video = Video.find(params[:video_id]) @vote = @video.video_votes.new @vote.user = current_user if params[:type] == "up" @vote.value = 1 else @vote.value = -1 end if @previous_vote.nil? if @vote.save respond_to do |format| format.html { redirect_to @video } format.js end

Groovy static generic type

只愿长相守 提交于 2019-12-12 01:54:32
问题 I've been playing around with getting rid of DAOs in favor of ActiveRecord like entities in Java, but generics won't allow me to have the full functionality that I want (like static finders). Somehow Groovy does, but I'm confused why. Given the following: class ActiveRecord<T> { static Class<T> genericType; ActiveRecord() { genericType = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; } static T find(Serializable id) { return EntityManagerHolder.find

ActiveRecord Sum Max Joins results in array with multiple nil ids

送分小仙女□ 提交于 2019-12-12 01:50:11
问题 Same Question as Rails activerecord : sum, max and joins except getting strange results. class CustomResumeline < ActiveRecord::Base has_many :resumeline_words end class ResumelineWord < ActiveRecord::Base belongs_to :custom_resumeline end CustomResumeline.joins(:resumeline_words).where(resumeline_words: {name: ["time", "data"]}).select('sum(resumeline_words.weight) as nb_votes').group('resumeline_words.custom_resumeline_id').order('nb_votes ASC') Results CustomResumeline Load (0.8ms) SELECT

ActiveRecord Group and Count - Where Does count_id Come From?

假装没事ソ 提交于 2019-12-12 01:48:53
问题 I have Order and Item models joined by OrderItem : class OrderItem < ActiveRecord::Base belongs_to :order belongs_to :item This query finds the IDs of the most popular items: OrderItem.group(:item_id).order("count_id DESC").count("id").first(10).map(&:first) This works, but what is count_id ? Why does this work? What is going on here behind the scenes with order("count_id... ? 回答1: count_id is an alias that Rails gives to the SQL aggregate function COUNT("order_items"."id") . OrderItem.group(

Strange error while accessing active record relation

徘徊边缘 提交于 2019-12-12 01:47:02
问题 I have these models: class Segment < ActiveRecord::Base has_many :families, :foreign_key => :segment_code, :primary_key => :code end class Family < ActiveRecord::Base belongs_to :segment, :foreign_key => :segment_code, :primary_key => :code has_many :genus, :foreign_key => :family_code, :primary_key => :code end class Genus < ActiveRecord::Base belongs_to :family, :foreign_key => :family_code, :primary_key => :code has_many :commodities, :foreign_key => :genus_code, :primary_key => :code end

Casting problem in SubSonic 3

孤街浪徒 提交于 2019-12-12 01:42:31
问题 When saving a row that has a integere primary key following exception is thrown in the VB version: 'Public member 'ChangeTypeTo' on type 'Decimal' not found.' This happens in ActiveRecord.VB file line 3406: Public Sub SetKeyValue(value As Object) Implements IActiveRecord.SetKeyValue If value IsNot Nothing AndAlso value IsNot DBNull.Value Then Dim settable = value.ChangeTypeTo(Of Integer)() I can change the last line to: Dim settable = CInt(value) 'value.ChangeTypeTo(Of Integer)() This will