Tag: activerecord

Rails属于头痛

新手铁路问题即将来临。 我有一个这样的课: class Thing false end 我做了一次迁移,表格看起来还不错。 然后我启动rails console并尝试以下操作: t = Thing.new(:name => “test”) => # 已经在这里它说名字是零,为什么? 继续,我试试这个: t.name => “test” 现在名字似乎设置了吗? 如果我试图保存: t.save! Thing Exists (8.0ms) SELECT 1 AS one FROM “things” WHERE LOWER(“things”.”name”) = LOWER(‘test’) LIMIT 1 SQL (16.0ms) INSERT INTO “things” (“created_at”, “description”, “name”, “updated_at”) VALUES (‘2012-10-28 16:10:12.701000’, NULL, NULL, ‘2012-10-28 16:10:12.701000’) […]

rails模型范围中的关联数据

我有一个名为Post (博客文章)的模型和一个名为Category的模型。 每个postbelongs_to一个类别。 每个类别都有一个名为retainer的属性,用于指定post“过期”之前的时间量,例如movies_category.retainer = 30.days 我要做的是为Post创建一个范围,找到所有“过期”的post。 因此,例如,假设我要对30.days的值进行硬编码并且它适用于所有类别(因此所有post),范围将是: scope :expired, lambda { where(“posts.created_at < ?", 30.days.ago) } 但是,我没有对值30.days.ago进行硬编码,而是希望从post的类别中获取retainer值,并根据该条件确定条件,例如: scope :expired, lambda { where(“posts.created_at < ?", Time.now – post.category.retainer) } 所以说得好:我希望获得所有过期的post,并且过期状态由每个post的类别的保留值确定(即电影类别中的post在10天后到期,游戏类别中的post在5天后到期等) 这有可能吗? 我需要某种forms的加入吗?

Activerecord多态机制如何用于MTI实现?

我发现这个QA 为什么Rails中的ActiveRecord不支持多表inheritance? 现在我根据CdotStrifeVII的答案提出了一个问题 多态关联填补了大多数用例的作用 你同意吗? 至于我,多态更像是STI实现…… 如何使用rails多态机制来实现MTI?

Rails的开发和生产环境之间有什么重要的区别?

由于Rail的生产和开发环境之间存在差异,我今天遇到了一个可怕的问题。 考虑一下代码: “select * from subscription_plans where affiliate_id is null or affiliate_id = #{@subscription_plan.affiliate.id rescue 0};” 永远不会有任何id为0的关联公司,所以如果@ subscription_plan.affiliate是nill,我希望查询只返回没有关联公司的订阅计划。 在开发环境中工作得很好,因为nil.id会抛出一个错误(前提是它确实给出了一些关于它的消息应该错误地为4)。 问题是,我将该代码实时推送到我的生产服务器,并且affiliate_id为4的订阅计划开始全部显示。 在生产中,nil.id不会抛出错误,而是简单地返回4. Geez,谢谢rails。 所有这些问题,作为Rails开发人员,我应该注意哪些其他事项? 特别是,环境之间是否存在可能导致问题的其他差异?

如何将BigDecimal与ActiveRecord十进制字段进行比较?

假设这样的架构: create_table “bills”, :force => true do |t| t.decimal “cost”, :precision => 10, :scale => 5 end 我想写一个函数,如果它是唯一的,它会将新的账单写入数据库。 以下不起作用: def load_bill_unless_exists(candidate) incumbents = Bill.scoped.where(:cost => candidate.cost) candidate.save unless incumbents.exists? end 因为现任账单和候选账单的BigDecimal表示具有不同的限制,所以:cost => candidate.cost测试失败。 也就是说,它正在比较: candidate: #<Bill id: nil, cost: #> 同 incumbent: #<ServiceBill id: 198449, cost: #> 请注意,候选人的BigDecimal表示比现任者更多的数字。 所以问题很简单:进行这种比较的正确方法是什么? 我考虑过:cost => BigDecimal.new(candidate.cost.to_s, 18) ,但这感觉不对 – […]

map(&:id)work但not pluck(:id)

在我的部分代码中,我需要获取用户的id,但是pluck不会抓住它们。 只有map(&:id)可以做到。 我想知道为什么。 我写了一个快速而又脏的代码块来查找发生了什么 def remove_users user_ids p users_to_remove.class users_to_remove = self.users.where(id: user_ids) if users_to_remove.present? self.users -= users_to_remove self.save p users_to_remove.class p users_to_remove Rails::logger.info “\n#{users_to_remove}\n” users_to_remove_map = users_to_remove.map(&:id) p users_to_remove_map Rails::logger.info “\nmap id: #{users_to_remove_map}\n” users_to_remove_pluck = users_to_remove.pluck(:id) p users_to_remove_pluck Rails::logger.info “\npluck id: #{users_to_remove_pluck}\n” #… end self.user_ids end 谁在我的test.log中返回 # map id: [144004] (0.3ms) SELECT “users”.”id” […]

获取Rails ActiveRecord’datetime’属性作为DateTime对象

我的一个模型中有一个包含日期/时间值的属性,并在我的迁移中使用t.datetime :ended_on声明。 当我使用myevent.ended_on获取此值时,我得到一个Time对象。 问题是,当我尝试将此属性用作Flotilla图表中的轴时,它无法正常工作,因为Flotilla仅将日期识别为Date或DateTime对象。 我想过编写一个将现有Time值转换为DateTime的虚拟属性,但是我很担心这样做,因为我听说时间不能处理2040年以后的日期,我不希望有可能造成“2040虫子”后来担心。 有没有什么办法可以说服ActiveRecord为这个属性而不是Time对象返回DateTime对象?

DISTINCT ON查询w / ORDER BY列的最大值

我的任务是将一个Rails应用程序从MySQL转换为Postgres asap并遇到一个小问题。 活动记录查询: current_user.profile_visits.limit(6).order(“created_at DESC”).where(“created_at > ? AND visitor_id ?”, 2.months.ago, current_user.id).distinct 生成SQL: SELECT visitor_id, MAX(created_at) as created_at, distinct on (visitor_id) * FROM “profile_visits” WHERE “profile_visits”.”social_user_id” = 21 AND (created_at > ‘2015-02-01 17:17:01.826897’ AND visitor_id 21) ORDER BY created_at DESC, id DESC LIMIT 6 我在使用MySQL时非常自信,但我老实说是Postgres的新手。 我认为这个查询失败的原因有很多。 我认为需要先区分。 我不知道如何通过max函数的结果来订购 我甚至可以使用这样的max函数吗? 此查询的高级目标是返回用户的6个最新配置文件视图。 任何关于如何修复此ActiveRecord查询(或它的结果SQL)的指针将不胜感激。

通过深入了解关系来获得数量

我正在使用Rails 4,我希望获得针对特定post的每个post评论的计票数。 架构: Post PostReview PostReviewVote id post_id post_review_id user_id user_id voted_on_date 我有类似的东西: table.table.table-striped.table-hover thead tr th Post Name th Reviews Created For Post th Total Votes in All Reviews For Post tbody – for post in @posts tr td= post.name td= PostReview.where(post_id: post.id).size td= PostReview.where(post_id: post.id).PostReviewVotes.size 但它并没有提取,引用了运行时错误: undefined method `PostReviewVotes’ for # 有什么建议? 我的模型有适当的关联,仅供参考。

Rails4:模型缓存(低级缓存)

我正在尝试缓存Active Record Query的结果。 这是我的用例: User.rb def self.awesome_users_cached Rails.cache.fetch(“awesome_users”) { where(awesome: true) } end UsersController def index all_awesome_users = User.awesome_users_cached less_awesome_users = User.where(less_awesome_user:true).pluck(:id) @users = all_awesome_users.where.not(id: less_awesome_users) end 这是2个问题: self.awesome_users_cached不会缓存结果并最终命中 all_awesome_users最终成为一个数组而不是活动Record,因此我无法从中过滤掉less_awesome_users。 有一个解决方案似乎不再适用于Rails4: Rails缓存与where子句