Tag: activerecord

在has_many上通过:关联优雅地设置连接表属性

在我的Rails 3.2应用程序中, Users可以创建Articles Collections 。 Collections和Articles由has_many through: collections_articles关系连接。 我希望在将Article添加到Collection时将user_id存储在collections_articles连接表中。 (因此该应用程序可以稍后显示谁将文章添加到集合中)。 有一种优雅的方式来做到这一点? class CollectionsArticle collection_id article_id user_id belongs_to :collection belongs_to :article belongs_to :user 当用户将文章添加到集合中时,我只想使用铲子,因此: my_collection.articles << my_article 无论如何(优雅地)同时将连接表的user_id设置为current_user ? 或者是在连接表本身中显式创建记录的最佳方法: CollectionsArticle.create(article: @article, collection: @collection, user: current_user) 谢谢!

Heroku + Rails错误ActiveRecord :: StatementInvalid

我在这里遇到了一些困境。 我有一个评论脚手架和一个电影脚手架 当去电影页面并创建新评论时,我去这里/ movies / 12 / comments / new(12是id) 并且做了我的控制器 def new @movie = Movie.find(params[:movie_id]) @comment = @movie.comments.new @search = Movie.search(params[:q]) respond_to do |format| format.html # new.html.erb format.json { render json: @comment } end end 但是当我启动到heroku时,我得到了这个错误 *Processing by CommentsController#new as HTML ActiveRecord::StatementInvalid (PG::Error: ERROR: invalid input syntax for integer: “movie_id” Completed 500 Internal Server […]

多态关联和设置资产的默认值

新手问题。 我有以下型号: class Asset true #paperclip has_attached_file :asset, :hash_secret => “my-secret”, :url => “/images/:hash_:basename_:style.:extension”, :path => UPLOAD_PATH + “/:hash_:basename_:style.:extension”, :styles => { :medium => “300×300>”, :thumb => “75×75>” } end class Location :assetable, :dependent => :destroy end class MenuItem :assetable end 我的资产有一个叫做描述的属性。 如果assetable_type是“MenuItem”并且描述为nil,我希望描述是关联的menu_item的主体。 我该怎么办? 谢谢

模型属性不会更新,在不更新的单独属性上调用validation错误?

我有以下半高级数据库查询,该查询过去10年的每小时价格并返回过去七天的每日平均价格: averages = Trade.where(‘date >= ?’, 7.days.ago).average(:price, :group => “DATE_TRUNC(‘day’, date – INTERVAL ‘1 hour’)”) 这将返回date (当天)和averageprice如下所示: “2012-12-29 00:00:00″=># 然后我遍历每个响应并将它们保存为TradeDailyAverage模型中的新记录。 # Loops through each daily average produced above averages.each do |date, avg| # Converts the BigDecimal to Floating Point(?) averagefloat = avg.to_f # Rounds the Daily Average to only two decimal points dailyaverage = number_with_precision(averagefloat, […]

识别没有与SQL查询的支付关联的资源

我有一个索引页面,我想要显示特色和标准板。 特色板有一个付款,标准板没有。 class Payment < ApplicationRecord belongs_to :board end class Board < ApplicationRecord has_one :payment end 因此,我可以通过加入付款来识别特色板,从而删除标准板。 Board.joins(:payment).where(category: category, confirmed: true) 现在我想通过以下方式获得标准列表: Board.where(category: category, confirmed: true) 但这两者都返回了特色和标准板。 我正在寻找一种方法来获得标准列表(没有付款的电路板),我只是无法弄清楚如何做到这一点。

Rails Active Record Query仅加入,任何

Student has_many :enrollments 通过此查询,我看到那些具有true注册的学生,以及同时具有false注册的学生: @students = Student.joins(:enrollments).where(enrollments: { is_active: false }) 是否有一些“仅”属性我可以添加以查看只有活跃注册的学生?

强制活动模型序列化程序返回关联

我有一个Active Model Serializer ,它具有以下function: class API::DashboardSerializer < ActiveModel::Serializer attributes :id, :name, :special def special x = object.check_ins.first prev = x.prev_ci_with_weigh_in end end special返回CheckIn类的记录,我希望它为该记录使用CheckInSerailizer 。 如何强制它使用special的CheckInSerializer ?

奇怪的舍入问题

使用小数和浮点数发生了一些非常奇怪的事情,我无法理解为什么或在哪里(ruby / rails / postgresql)。 给定带有小数列的购买表 – 总计 : 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] 无论小数或浮点数,Ruby和postgresql都没有完全代表5.99的问题: 5.99.to_s # => “5.99” 5.99.to_d.to_s # => “5.99” 5.99 == 5.99.to_d # => true SELECT CAST(5.99 AS […]

Rails:基于多个参数过滤?

我有一个简单的应用程序,用户可以按类别,纪律和目标组基于三个不同的参数进行过滤。 所有参数都是可选的。 到目前为止它的工作原理,但下面的控制器有点臃肿,我也认为这三个if语句有点乱。 有更清洁的方法吗? def index @listings = Listing.includes(:categorizations, :listing_disciplines, :listing_targets).page(params[:page]) if params[:category_id].present? && params[:category_id] != “” @category_id = Category.find_by(id: params[:category_id]) @listings = @category_id.listings end if params[:discipline_id].present? && params[:discipline_id] != “” @discipline_id = Discipline.find_by(id: params[:discipline_id]) @listings = @discipline_id.listings end if params[:target_id].present? && params[:target_id] != “” @target_id = Target.find_by(id: params[:target_id]) @listings = @target_id.listings end if params.blank? […]

Rails :: ActiveRecord在深拷贝副本中保留auto_increment id

我需要创建一个包含要操作的模型对象的临时数组。 我需要auto_increment主键,但我不希望保持关联,因为当我从数组中删除它时,它也将它从数据库中删除(感谢rails,非常不方便)。 我使用object.dup创建的深层副本的id标记为nil。 如何将它们保存在副本中?