activerecord

ActiveRecord: keep duplicated objects in .where query

只谈情不闲聊 提交于 2020-01-04 10:22:16
问题 I have an array of ids: ids = [1, 2, 3, 1] If I run: Product.where(id: ids) I will only get one occurence of the Product having the iD 1 . However, I'd like to get as many occurences of the object as there are in the array. How can I do this ? 回答1: You could load the unique records and then transform the result into an Array with multiple references to each book: ids = [2109, 2511, 2108, 2109] tmp_books = Book.find(ids) new_books = ids.map {|x| tmp_books.detect {|b| b.id == x } } That

ActiveRecord Fuzzy Search

独自空忆成欢 提交于 2020-01-04 09:11:53
问题 I'm looking to perform a search to find an object similar to this: Object(id: 1, example: "abc") by using a search like this: params[:search] = "abcdef" Object.where("example LIKE ?", "#{params[:search]%") but am only able to use the above example if my search has less characters than my object, not more. 回答1: I think it should be params[:search] = "abcdef" Object.where("example LIKE ?", "%#{params[:search]}%") Also might want to use ilike for case insensitive search (if you're using postgres

ActiveRecord Fuzzy Search

主宰稳场 提交于 2020-01-04 09:11:37
问题 I'm looking to perform a search to find an object similar to this: Object(id: 1, example: "abc") by using a search like this: params[:search] = "abcdef" Object.where("example LIKE ?", "#{params[:search]%") but am only able to use the above example if my search has less characters than my object, not more. 回答1: I think it should be params[:search] = "abcdef" Object.where("example LIKE ?", "%#{params[:search]}%") Also might want to use ilike for case insensitive search (if you're using postgres

Role-dependent associations

左心房为你撑大大i 提交于 2020-01-04 09:07:32
问题 I am working on my first real Rails app for timetracking in a company and I have run into database/model design concern. My app has a User model and a Role model which are linked together through a has_and_belongs_to_many association. One of the roles is the manager. A manager can have many assistants and assistants can have many managers (although typically only one). This relationship is represented through a manager_assistant_relationship expressed in the User model as: has_many

Enumerated Types with ActiveRecord and Postgresql

眉间皱痕 提交于 2020-01-04 07:49:26
问题 I am following this tutorial from SitePoint to set a model property to an Enum value, which is supported by Rails as of 4.1. Instead of Gender Enum, I am trying to add a Season Enum. This is the issue I get in my schema.db # Could not dump table "semesters" because of following StandardError # Unknown type 'season' for column 'season' This is my migration : class AddSeasonToSemesters < ActiveRecord::Migration[5.1] def up execute <<-SQL CREATE TYPE season AS ENUM ('fall', 'winter', 'spring',

ActiveRecord: abort datetime parsing if value is invalid

*爱你&永不变心* 提交于 2020-01-04 06:20:39
问题 I have a datetime field in my ActiveRecord model (in Rails) and I need to protect my model from crashes when date fieid is out of ranges. I found this question and and tried to make similarly: class Something < ActiveRecord::Base validate :important_date_is_valid_datetime, on: :create validates :important_date, presence: true private def important_date_is_valid_datetime if (DateTime.parse(important_date.to_s()) rescue ArgumentError) == ArgumentError then errors.add(:important_date, 'must be a

how to implement RoR many-to-many relationship?`

孤人 提交于 2020-01-04 06:11:15
问题 I have the following models: Technician - id - name - personal_id Tool - id - internal_code - name - category A technician can have many tools and one tool can be assigned to many technician (do I really need many-to-many relationship, as I'm typing this I have my doubts). My problem is that I also need to track the quantity of the tool that the technician have under his possession. One record should be like [technician_id, tool_id, quantity]. I have read about the has_many through, but i

how to implement RoR many-to-many relationship?`

↘锁芯ラ 提交于 2020-01-04 06:10:07
问题 I have the following models: Technician - id - name - personal_id Tool - id - internal_code - name - category A technician can have many tools and one tool can be assigned to many technician (do I really need many-to-many relationship, as I'm typing this I have my doubts). My problem is that I also need to track the quantity of the tool that the technician have under his possession. One record should be like [technician_id, tool_id, quantity]. I have read about the has_many through, but i

codeigniter - pyrocms intercept and modify all queries; extending active record

淺唱寂寞╮ 提交于 2020-01-04 04:14:18
问题 Is there a way to catch all queries and modify them before being sent to the db outside of modifying DB_active_record.php ? This would be ideal, though I'm not averse to modifying this file if it's the only option. I could just call this stuff from the models, but would feel better if this was something that was done in the background so as not to leave room for forgetting when and where it's happening; it'd be best to have it done in the background. I'm using a library to encrypt query data.

Rails: How to query time range, not date, values for a model in activerecord

霸气de小男生 提交于 2020-01-04 01:57:10
问题 I have a model with a time attribute. It's configuration for when a user wants to receive an email, i.e., 5 PM EST. It's stored in the database as 21:00:00 . I'd like to query it by range. For example, I'd like every user with a reminder time between 20:55:00 and 21:05:05 . Rails seems to handle time attributes kind of strangely because it wants to show it as a full DateTime, so the database has 21:00:00 but when loaded in rails, it shows it as 2000-01-01 17:00:00. My code right now looks