activerecord

How do I replace accented Latin characters in Ruby?

六月ゝ 毕业季﹏ 提交于 2019-12-17 03:53:48
问题 I have an ActiveRecord model, Foo , which has a name field. I'd like users to be able to search by name, but I'd like the search to ignore case and any accents. Thus, I'm also storing a canonical_name field against which to search: class Foo validates_presence_of :name before_validate :set_canonical_name private def set_canonical_name self.canonical_name ||= canonicalize(self.name) if self.name end def canonicalize(x) x.downcase. # something here end end I need to fill in the "something here"

Rails 5: ActiveRecord OR query

人盡茶涼 提交于 2019-12-17 03:34:25
问题 How do you do an or query in Rails 5 ActiveRecord? Also, is it possible to chain or with where in ActiveRecord queries? 回答1: The ability to chain or clause along with where clause in ActiveRecord query will be available in Rails 5 . See the related discussion and the pull request. So, you will be able to do the following things in Rails 5 : To get a post with id 1 or 2: Post.where('id = 1').or(Post.where('id = 2')) Some other examples: (A && B) || C: Post.where(a).where(b).or(Post.where(c))

Rails 3: Get Random Record

六眼飞鱼酱① 提交于 2019-12-17 02:58:38
问题 So, I've found several examples for finding a random record in Rails 2 -- the preferred method seems to be: Thing.find :first, :offset => rand(Thing.count) Being something of a newbie I'm not sure how this could be constructed using the new find syntax in Rails 3. So, what's the "Rails 3 Way" to find a random record? 回答1: Thing.first(:order => "RANDOM()") # For MySQL :order => "RAND()", - thanx, @DanSingerman # Rails 3 Thing.order("RANDOM()").first or Thing.first(:offset => rand(Thing.count))

ActiveRecord, has_many :through, and Polymorphic Associations

泪湿孤枕 提交于 2019-12-17 02:53:06
问题 Folks, Want to make sure I understand this correctly. And please disregard the case for inheritance here (SentientBeing), trying to instead focus on polymorphic models in has_many :through relationships. That said, consider the following... class Widget < ActiveRecord::Base has_many :widget_groupings has_many :people, :through => :widget_groupings, :source => :person, :conditions => "widget_groupings.grouper_type = 'Person'" has_many :aliens, :through => :widget_groupings, :source => :alien,

How to do a LIKE query in Arel and Rails?

会有一股神秘感。 提交于 2019-12-17 02:40:09
问题 I want to do something like: SELECT * FROM USER WHERE NAME LIKE '%Smith%'; My attempt in Arel: # params[:query] = 'Smith' User.where("name like '%?%'", params[:query]).to_sql However, this becomes: SELECT * FROM USER WHERE NAME LIKE '%'Smith'%'; Arel wraps the query string 'Smith' correctly, but because this is a LIKE statement it doesnt work. How does one do a LIKE query in Arel? P.S. Bonus--I am actually trying to scan two fields on the table, both name and description, to see if there are

(推荐使用)提高开发效率工具集合

老子叫甜甜 提交于 2019-12-14 16:27:42
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 提高开发效率工具集合(推荐使用) 一、Hutool工具类 官网地址:https://www.hutool.cn/ Github地址:https://github.com/looly/hutool/ Gitee 地址:https://gitee.com/loolly/hutool/ 文档参考地址:https://www.hutool.cn/docs/index.html 开源协议许可: MulanPSL 使用方式:引入jar包即可(具体使用参考文档和API) Hutool工具类是一个Java基础工具类,对文件、流、加密解密、转码、正则、线程、XML等JDK方法进行封装,组成各种Util工具类,同时提供以下组件: 1. 布隆过滤 2. 缓存 3. 数据库ORM(基于ActiveRecord思想) 4. HTTP客户端 5. IO 6. JSON 7. 日志 8. System(JVM和系统信息等) 9. Setting(一种扩展Properties的配置文件)等。 关注该项目已经三年多,目前多个公司正在使用中,并且在持续更新中。拿过来直接可以使用,用于解决项目中工具类不全,区分不明确的问题,实现快速开发,专注于业务实现和优化。 二、Lombok 包 官网地址;https://projectlombok.org/

SyntaxError: (irb):26: both block arg and actual block given

旧城冷巷雨未停 提交于 2019-12-14 03:52:57
问题 I have this query = f.select(:city, Country.where(:country_code => "es").collect(&:cities) {|p| [ p.city, p.id ] }, {:include_blank => 'Choose your city'}) Problem is I'm getting the following error SyntaxError: (irb):26: both block arg and actual block given From what I see I'm doing something wrong by including the collect(&:cities) and then declaring the block. Is there a way I can accomplish both with same query? 回答1: Country.where(:country_code => "es").collect(&:cities) is exactly the

Stop child models updating when parent is updated

那年仲夏 提交于 2019-12-14 03:15:51
问题 I have an Order/LineItem type application which inserts an Order and its associated Line Items into a database. After the order is created (after_create) I then call a function which updates a totals field in the Order table. However, when this runs it also runs an update query on all the line items, and because a paper_trail is active for :update, it causes new versions to be inserted too. The problem is, there can be hundreds of line items (the largest so far was 872), so obviously this isn

Rails3 ActiveRecord - Fetching all records that have A and B (through a has_many relationship)

醉酒当歌 提交于 2019-12-14 02:38:55
问题 I have the following model relationships: class Article < ActiveRecord::Base has_many :tags, :through => :article_tags end class ArticleTag < ActiveRecord::Base belongs_to :tag belongs_to :article end class Tag < ActiveRecord::Base has_many :articles, :through => :article_tags end Now I want to load all the Articles that are tagged with both tag "A" and tag "B". I'm pretty new to Rails and it has been a few years since I did any serious web-dev/SQL work but for the life of me I can't figure

With Rails 5, how do I add a database record after a new User has been created?

馋奶兔 提交于 2019-12-14 02:23:13
问题 In my application, I have Accounts, Users, and Permissions. When a new user is created, I have gotten as far as automatically creating an Account record with the new User foreign_key set as an "owner_id" in the Account table. At the same time this is done, I want to add a record to my Permissions table with the new account_id and new user_id set in their respective columns, as well as the resource column set to "Account" and the role column set to "owner". Here is how my models look: account