Tag: activerecord

‘includes’方法似乎不适用于Rails 4中的’where’

在我的模型中我有: class Song < ActiveRecord::Base belongs_to :artist def self.default_scope includes :artist end def self.search query if query where "title LIKE :query OR artists.name LIKE :query", query: "%#{ query }%" else where nil end end end 在我的控制器中: def index @songs = Song.search(params[:search]) respond_with(@songs) end 当我搜索时,我收到以下错误: Mysql2::Error: Unknown column ‘artists.name’ in ‘where clause’: SELECT `songs`.* FROM `songs` […]

Ruby on Rails – 基于查询在数据库中搜索

我有一个简单的表单,我在其中设置了一个我想浏览的查询,例如panasonic viera 。 这是我在数据库中搜索术语的方式: Product.where(“name ilike ?”, “%#{params[:q]}%”).order(‘price’) 该查询看起来像%panasonic viera% ,但我需要以这种方式搜索查询: %panasonic%viera% – 我需要找到所有产品,标题中的字段是panasonic或viera ……但是如何制作这个查询?

Rails ActiveRecord关系助手如何允许诸如`user.groups.new`之类的方法?

在Rails中,我创建了一个模型,用于从LDAP数据库而不是ActiveRecord中检索用户。 现在我尝试将我的ActiveRecord模型与基于LDAP的模型集成,因此我在模型中编写模拟一些常见ActiveRecord方法的方法。 我试图模仿的方法之一是通常由ActiveRecord上的has_many通过关系创建的方法。 在ActiveRecord中,此关系将允许以下内容: user = User.first groups = user.groups # == Array of Groups groups << Group.create(name: "Test") # How does Rails allow this? Rails究竟是如何允许的? 我已经尝试动态地将方法分配给user.groups返回的数组实例,但似乎没有任何方法可以让这些方法知道数组是从哪个用户记录创建的。 (因此他们可以在新的关系记录上分配user_id 。)我缺少什么?

rails如何正确地“更新”多个列 – (updated_at也应该更新)

我无法使用rails activerecords正确更新多个列。 我想使用像更新基本上更新updated_at的东西,但我不能通过多列。 我可以使用update_all但它不会使用当前时间戳更新updated_at。 这就是我想要的: 这不起作用:(只接受payment_total但不接受其余部分) retval = @invoice.update(:payment_total => 20, :due_amount => 10, :data => clonedata.to_json) :data – this is actually a json field 输出: nothing 这工作: retval = Invoice.where(:id => @invoice.id).update_all(:payment_total => 20, :due_amount => 10, :data => clonedata.to_json) 输出:(注意它没有更新“updated_at”字段) SQL(1.1ms)UPDATE“发票”SET“payment_total”= ’30 .00’,“due_amount”= ’86 .00’,“data”='{“名称”:“测试}’WHERE”发票“。”id“= 6 单个参数可以工作: retval = @invoice.update(:payment_total => 20) 输出: […]

HABTM关系查找所有记录,不包括基于关联的记录

我看过一些与此相关的类似SOpost,但我很难理解它。 我在项目和用户之间有一种关系。 我试图找到特定用户不属于的所有项目,但我不知道如何。 我尝试过这样的事情: Project.where(‘project_id != ?’, user.id) 但这显然也是错的。 我正在使用rails 3.2.x. 许多与此相关的答案都提到了范围,但我之前没有遇到过它们(我还是Rails的新手)。 我刚发现这篇文章有一个答案暗示: Project.where(‘id not in (?)’, user.projects) 这似乎有效,除非user.projects为空。 我正在尝试Project.where(‘id not in (?)’, (d.projects.empty? ? ”, d.projects))正如JosephCastro的回答评论post中所建议的那样,但它给了我一个语法错误第二个d.projects 。 编辑 与用户相关的项目模型代码段 class Project :validates_unique 然后 class User < ActiveRecord::Base attr_accessible … has_and_belongs_to_many :projects

如何查询postgres的UUID

我想使用UUID作为标识符,提供前8位数字以确定它是否存在于数据库中。 通常我可以毫无问题地做到这一点: select * from TABLE where id = ‘e99aec55-9e32-4c84-aed2-4a0251584941’::uuid 但这给了我错误: select * from TABLE where id LIKE ‘e99aec55%@’::uuid 错误: ERROR: invalid input syntax for uuid: “e99aec55%@” LINE 1: select * from TABLE where id LIKE ‘e99aec55… ^ Query failed PostgreSQL said: invalid input syntax for uuid: “e99aec55%@” 有没有办法在postgresql中查询UUID类型的前n位数?

Rails:调用“id”主键方法时出现“Stack level too deep”错误

这是另一个问题的转贴,这次更好地隔离了。 在我的environment.rb文件中,我改变了这一行: config.time_zone = ‘UTC’ 到这一行: config.active_record.default_timezone = :utc 从那以后,这个电话: Category.find(1).subcategories.map(&:id) 在config.cache_classes = false后第二次在开发环境中运行时,“堆栈级太深”错误失败。 如果config.cache_classes = true,则不会发生此问题。 该错误是第252行的active_record / attribute_methods.rb中的以下代码的结果: def method_missing(method_id, *args, &block) … if self.class.primary_key.to_s == method_name id …. 对“id”函数的调用会重新调用method_missing,并且没有任何东西阻止id被反复调用,从而导致堆栈级别太深。 我正在使用Rails 2.3.8。 Category模型has_many:subcategories。 调用在上面那行的变体上失败(例如,Category.first.subcategory_ids,使用“each”而不是“map”等)。 任何想法将受到高度赞赏。 谢谢! 阿米特

db:种子不加载模型

我正在尝试使用标准db/seeds.rb方法为我的数据库设定种子。 这在我的开发机器上工作正常,但在我的服务器上,我得到: $ sudo rake db:seed RAILS_ENV=production –trace ** Invoke db:seed (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute db:seed rake aborted! uninitialized constant Permission /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing’ /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:92:in `const_missing’ /path/…/…/…/…/db/seeds.rb:4 /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/tasks/databases.rake:215:in `load’ /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/tasks/databases.rake:215 /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call’ … 但是当我在控制台中检查时,模型确实存在: $ script/console production Loading production environment (Rails 2.3.4) >> Permission => Permission(id: integer, …, created_at: datetime, […]

在Rails中,如何在新对象的Create方法中自动更新用户以拥有另一个类的新创建对象?

我的应用程序有Users和Dwellings 。 当user创建dwelling , user通过铁轨协会成为dwelling的owner 。 通过我的create方法, user_id被成功分配给owner_id列中新创建的dwelling ,但是dwelling_id不会传播到user记录中的dwelling_id 。 我不确定是否是错误设置的关系或方法,但是当成功创建dwelling时,尝试保存user时似乎会出现问题。 以下是我的模型和方法实现。 住宅模型 # dwelling.rb class Dwelling “User”, :foreign_key => “owner_id” has_many :roomies, :class_name => “User” validates :street_address, presence: true validates :city, presence: true validates :state, presence: true validates :zip, presence: true end – 用户模型 # user.rb class User “Dwelling”, :foreign_key => “owner_id” … – […]

Rails模型。有效吗? 刷新自定义错误并错误地返回true

我试图将自定义错误添加到我的用户模型的实例,但是当我调用有效时? 它正在擦除自定义错误并返回true。 [99] pry(main)> u.email = “test@test.com” “test@test.com” [100] pry(main)> u.status = 1 1 [101] pry(main)> u.valid? true [102] pry(main)> u.errors.add(:status, “must be YES or NO”) [ [0] “must be YES or NO” ] [103] pry(main)> u.errors #[“must be YES or NO”]}> [104] pry(main)> u.valid? true [105] pry(main)> u.errors # 如果我在模型中使用validate方法,那么它可以工作,但是这个特定的validation是从一个不同的方法中添加的(这需要传递参数): User def do_something_with(arg1, arg2) […]