Tag: activerecord

从数据库中选择,分组和求和结果

我有一个数据库,其中包含一些我要总结的字段。 但这不是大问题,我想按照它们创建的月份对这些字段进行分组。 ActiveRecord自动创建了一个名为“created_at”的字段。 所以我的问题; 如何按月对结果进行分组,然后对每个月的字段求和? 更新了代码 @hours = Hour.all(:conditions => “user_id = “+ @user.id.to_s, :group => “strftime(‘%m’, created_at)”, :order => ‘created_at DESC’) 这是我现在的代码。 管理按月分组,但不能总结我的两个字段,“分钟”和“薪水”我需要总结

Rails 3序列化问题

我有一个适用于ror 2.3.x的应用程序。 我无法使用序列化升级到Rails 3。 代码看起来像这样 class PaymentTransaction < ActiveRecord::Base serialize :response end 响应应该包含ActiveMerchant :: Billing :: Response。 由于某种原因,使用rails 3将其保存为字符串。 => #<PaymentTransaction id: 11, order_id: nil, amount: nil, mode: nil, payment_profile_id: nil, response: "#”, created_at: “2010-11-07 04:06:03”, updated_at: “2010-11-07 04:24:58”, result: “pending”, payee: nil, login_id: nil, transaction_key: nil> 在任何其他关于升级的博客中,我都没有关于序列化的任何注释。 有什么想法吗?

Rails – 检查has_many关联中是否存在记录

我不确定我的问题措辞是否正确。 我有三个模型: User , Item和UserItem 。 user has_many :user_items user has_many :items, through :user_items item has_many :user_items item has_many :users -> {uniq}, through :user_items item belongs_to :user user_item belongs_to :user user_item belongs_to :item 我需要一种方法来查看用户是否有一个项目来制作我的项目视图中的语句但是这里是catch,user_items具有enum status: [ :pending, approved] 。 所以我需要查看current_user是否具有某个:pending项。 例如,当用户访问item1的视图页面时,我有item_controller的show动作声明@item = Item.find_by_id(params[:id]) 。 但是,我可以用这个@item来查看用户是否有这个项目?

Rails 3 ActiveRecord链接

我是Rails的新手,我不确定为什么我的链接不起作用。 工作 my_model.select(‘name’).where(‘status_id = 6’).all 不工作 my_model.select(‘name’).where(‘status_id = 6’).order(‘name’) 为什么where不执行查询的where之后链接order ? 我尝试在.all之后添加.order但这似乎也没有用。

ProtocolViolation:ERROR:绑定消息提供0个参数,但是准备好的语句“”需要1

我正在尝试根据最先留下最新评论的患者的顺序创建一份留下评论的独特患者列表。 这是我创建列表的Ruby .erb代码: @comment_list.order(“created_at desc”).each_with_index do |comment, index| @comment_list在控制器中定义为: @comments = current_clinician.comments.select(‘ON (patient_id) *’).uniq @comments = @comments.order(“patient_id, created_at DESC”) @comment_list = Comment.select(‘*’).from(“(#{@comments.to_sql}) sub”) 我收到一条ActiveRecord :: StatementInvalid消息: PG :: ProtocolViolation:错误:绑定消息提供0个参数,但是准备好的语句“”需要1:SELECT * FROM(SELECT DISTINCT ON(patient_id)* FROM“comments”WHERE“comments”。“clinician_id”= $ 1 ORDER BY patient_id, created_at DESC)sub ORDER BY created_at desc 我试着按照24619117的答案,我的输出是这个和答案顶部29660396的组合。 $ rails -v Rails 4.1.8 $ ruby -v ruby […]

如何确保不通过activerecords将重复的行添加到我的数据库表中?

所以我有一张叫做投票的桌子。 有三列。 VoterID,VoteforID和投票。 每个单独列的唯一性并不重要,但是同一行不能重复两次 例如。 这是对的 Voter_id | Votefor_id | 投票 1 | 2 | 1 1 | 3 | 1 1 | 4 | 1 2 | 1 | 2 这是错的。 Voter_id | Votefor_id | 投票 1 | 2 | 1 1 | 2 | 1 1 | 4 | 1 2 | 1 […]

Rails:需要帮助定义地址表的关联

我发现这有点弯曲,想要一些想法。 我的设计喜欢这个: 有构建者表,每个构建者都可以有一个邮政地址 有一个客户表,每个客户可以有一个邮政地址和账单地址 有一个地址表 我需要定义构建器地址和客户端地址之间的关联 我最初的数据库设计是这样的: Builders ——————– id postal_address_id … Clients ——————– id postal_address_id billing_address_id … Addresses ——————- id … 这对我来说似乎合乎逻辑,但我在rails关联中翻译它时遇到了麻烦。 我在想地址可以定义belongs_to:可寻址的多态关联。 但是如何处理客户的邮政和账单地址。 任何帮助都感激不尽。

以has_many关系订购

我的Articles有很多Metrics 。 当Metric.name =“得分”时,我希望通过特定的Metric.value来订购Articles 。 ( Metric将各种文章统计信息记录为“名称”和“值”对。文章可以有多个度量标准,甚至多个“分数”,尽管我只对最近的排序感兴趣。) class Article has_many :metrics class Metric # name :string(255) # value :decimal(, ) belongs_to :article 我正在努力写一个范围来做这个 – 任何想法? 像这样的东西? scope :highest_score, joins(:metrics).order(‘metrics.value DESC’) .where(‘metrics.name = “score”‘) 更新: 一篇文章可以在metrics量表中存储许多“分数”(因为它们是按周/每月/每年等计算的),但我只对使用任何一篇文章的第一个(最近的)“分数”感兴趣。 Metric模型具有default_scope,可确保DESCending排序。 修复了’metrics.value DESC’报价位置的拼写错误。 与我的手机朋友uber rails hacker交谈,看起来我可能需要一个原始的SQL查询。 现在,我已经超越了我的脑袋……(如果有帮助,我正在使用Postgres。) 谢谢! 更新2: 感谢Erwin的SQL查询建议,我有一个原始的SQL查询,它有效: SELECT a.* FROM articles a LEFT JOIN ( SELECT DISTINCT […]

在’has_many“到`关联上使用`find_or_create_by`时出错

我在has_many through关联使用find_or_create_by时遇到问题。 class Permission < ActiveRecord::Base belongs_to :user belongs_to :role end class Role :permissions end class User has_many :permissions has_many :roles, :through => :permissions end 当我在User对象的roles关联上调用find_or_create_by时,Rails会抛出错误。 u = User.first u.roles.find_or_create_by_rolename(“admin”) # Rails throws the following error # NoMethodError: undefined method `user_id=’ for # 我能够通过更改我的代码来解决此问题,如下所示: unless u.roles.exists?(:rolename => “admin”) u.roles << Role.find_or_create_by_rolename("admin") end 我很想知道find_or_create_by是否through关联与has_many工作。

ActiveRecord after_initialize with select

当我尝试调用ActiveRecord方法时,我的两个Rails模型select它只返回错误object doesn’t support #inspect 这些模型使用after_initialize回调,因此我怀疑它与此有关。 我正在使用像这样的after_initialize回调: enum state: [ :archived, :active ] after_initialize :state_init after_initialize :date_init def state_init self.state ||= :active end def date_init self.report_date ||= self.event ? self.event.event_date : Date.current end 我需要使用select方法选择按月分组的记录,使用与Postgres有日期的字段 ModelName.select(“date_trunc( ‘month’, report_date ) as month, count(*) as count”).group(‘month’) 有什么方法可以解决这个问题吗?