Tag: activerecord

需要使用范围嵌套连接的ActiveRelation

我是铁杆新手。 爆炸了。 但查询API给我带来了一些麻烦。 我一直在缩放并且很快就做了很多事情,但这是我第一次花费数小时试图解决这个问题。 它不像我之前使用的任何东西 – 常规SQL,或Hibernate,或其他任何东西。 我的模型非常简单。 PrivateMessage有许多收件人 收件人有一个接收者(类用户) 收件人还有’is_read’和’is_deleted’字段 我的目标是构建一个查询,查找给定用户的所有未读和未删除的私人消息。 为实现这一目标,我们需要将“private_messages”加入“收件人”……然后将“收件人”加入“用户”。 这是相关的用户模型代码: has_many :sent_messages, :class_name => ‘PrivateMessage’, :foreign_key => ‘sender_id’ has_many :recipient_of_messages, :class_name => ‘Recipient’, :foreign_key => ‘receiver_id’ scope :by_id, lambda { |id| where(:id => id) } 我的收件人模型具有以下相关代码: belongs_to :receiver, :class_name => ‘User’, :foreign_key => “receiver_id” belongs_to :private_message scope :unread, where(:is_read => false).where(:is_deleted […]

需要帮助来理解`has_and_belongs_to_many`

数据库中有三个表: 用户(非空) 时间表(非空) schedules_users( 空 ) 用户模型: class User < ActiveRecord::Base […] has_and_belongs_to_many :schedules#has_many :schedules […] end 时间表模型: class Schedule < ActiveRecord::Base has_and_belongs_to_many :users#belongs_to :user end welcome-controller 🙁我希望按日期和时间对日程安排进行排序) class WelcomeController < ApplicationController def index if(current_user) @user_schedules = current_user.schedules @user_schedules_date = @user_schedules.order(:date_time).group_by { |sched| sched.date_time.beginning_of_day } end end end @user_schedules = current_user.schedules这不正确,不是吗? SQL输出: Started GET “/” […]

Rails 3模型将某些列映射到不同的模型属性

我有一个名为“DXFTACCTS”的旧遗留表,我创建了Rails模型“Account”。 class Account < ActiveRecord::Base set_table_name "DXFTACCTS" end 问题是DXFTACCTS有像“XORFNAME”这样的字段,我希望它在模型中是“first_name”,依此类推。 如何将特定表列“映射”到模型属性? 谢谢!

ActiveRecord模糊搜索

我正在寻找搜索以找到类似于此的对象: Object(id: 1, example: “abc”) 通过使用这样的搜索: params[:search] = “abcdef” Object.where(“example LIKE ?”, “#{params[:search]%”) 但我只能使用上面的例子,如果我的搜索字符少于我的对象,而不是更多。

query:has_many其中所有子字段都是nil

我正在使用Rails 3.2与ActiveRecord和MySQL,我有一对多关联的模型: class Author has_many :books end class Book belongs_to :author attr_accessible :review end 我想找到没有审查所有书籍的作者。 我试过了: Author.includes(:books).where(‘book.review IS NIL’) 但显然没有用,因为它找到的作者至少有一本书没有经过审查。 我应该使用什么查询?

类的超类不匹配用户 – 从ActiveRecord :: Baseinheritance

我试图找出我的超类错配错误。 我读过的所有post都描述了这个问题,即User在我的应用程序中被定义为两次类。 就我而言,它没有定义两次。 我有一个服务文件夹,其中有一个用户文件夹(用于用户服务类)。 在该用户文件夹中,我有一个名为organisation_mapper_service.rb的文件,其中包含: class User < ActiveRecord::Base class OrganisationMapperService def self.call(user: u) new(user: user).call end def initialize(user: u) self.user = user end def call if matching_organisation.present? # user.organisation_request.new(organisation_id: matching_organisation.id) # user.update_attributes!(organisation_id: matching_organisation.id) else #SystemMailer.unmatched_organisation(user: user).deliver_now end end private attr_accessor :user def matching_organisation User::OrganisationMapperService.new(user).matching_organisation end end end 与此分开,我有我的用户模型,用户定义为: class User < ApplicationRecord 我认为以我的方式定义服务类应该没问题,因为它inheritance自ActiveRecord :: […]

升级到Rails 3.2.8导致与ActiveRecord的堆栈级别太深的错误

我对Ruby很新,但我遇到了一个问题,我无法深入了解问题。 我的简单应用程序工作正常,然后,在将Rails版本升级到3.2.8后,我在尝试查询数据库以查找记录时遇到了问题。 这在使用Rails 3.0.7时工作正常,但在升级到rails 3.2.8后,它开始失败并出现Stack Level太深的错误。 我的模型看起来像这样: class Resource true, :length => { :minimum => 5} validates :link, :presence => true validates :date_submitted, :presence => true has_many :comments validates_format_of :link, :with => URI::regexp(%w(http https)) validates :link, :uniqueness => {:scope => :link, :message => “This item has already been submitted”} end 当我从控制台调用Resource.all时,它失败并出现以下错误: SystemStackError:堆栈级别太深了/home/tom/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/workspace.rb:80也许IRB错误!! 它似乎也与其他型号一起发生,并且在升级之前这种工作正常的事实似乎表明它是另外的东西。 有没有人对我应该在哪里有任何想法 – […]

了解Ruby on Rails ActiveRecord模型访问器

我的模型,“DataFile”,有一堆字段,我想从模型外部设置,例如 file = DataFile.new file.owner = 123 现在,据我所知,我必须在我的模型中放置一个“attr_accessor:field”,用于我想从外部修改的每个字段。 但是,上面的代码运行正常,没有定义任何attr_accessors,将owner字段设置为123.为什么? 我希望得到一个“方法未定义”错误或类似的东西。

奇怪的问题has_many通过更新Rails中的关联

我从Ruby 2.1.2更新到Ruby 2.2.0并更新了链接到它的所有gem。 我有几个模型在彼此之间相互联系 class Question < ActiveRecord::Base belongs_to :course_version has_one :course, through: :course_version has_many :answer_questions has_many :answers, through: :answer_questions end class AnswerQuestion < ActiveRecord::Base belongs_to :answer belongs_to :question end class Answer < ActiveRecord::Base has_many :answer_questions has_many :questions, through: :answer_questions end 如您所知,我们有问题可以得到答案并通过answer_questions来了解他们得到了什么。 在我更新Ruby之前,它工作得很好。 现在我做的事情…… my_question.answers << my_answer 它确实爆炸了 NoMethodError: undefined method `name’ for nil:NilClass /Users/Loschcode/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:80:in […]

Rails 4按虚拟属性排序

我有一个Product模型,它在数据库中有name和description列。 我还有一个Product.search_results_for(query) ,其中query是一个像”Green Apple”这样的字符串。 我需要返回一个ActiveRecord::Relation ,其中排序的结果是最佳命中。 目前,我正在为每个产品设置search_result_value 。 search_result_value不是数据库中的列,我不希望它成为。 所以从本质上讲,我有一个ActiveRecord::Relation Products of Products ,我需要通过search_result_value来订购而不将其更改为数组,这是一个未存储在数据库中的实例变量。 我怎样才能做到这一点? 像这样的东西: Product.order(:search_result_value)