active-record-query

ActiveRecord, find by polymorphic attribute

扶醉桌前 提交于 2020-01-03 09:11:51
问题 Having this: class Event < ActiveRecord::Base belongs_to :historizable, :polymorphic => true end user = User.create! I can: Event.create!(:historizable => user) But I can't: Event.where(:historizable => user) # Mysql2::Error: Unknown column 'events.historizable' in 'where clause' I have to do this instead: Event.where(:historizable_id => user.id, :historizable_type => user.class.name) Update Code that reproduces the issue: https://gist.github.com/fguillen/4732177#file-polymorphic_where_test

ActiveRecord, find by polymorphic attribute

故事扮演 提交于 2020-01-03 09:11:06
问题 Having this: class Event < ActiveRecord::Base belongs_to :historizable, :polymorphic => true end user = User.create! I can: Event.create!(:historizable => user) But I can't: Event.where(:historizable => user) # Mysql2::Error: Unknown column 'events.historizable' in 'where clause' I have to do this instead: Event.where(:historizable_id => user.id, :historizable_type => user.class.name) Update Code that reproduces the issue: https://gist.github.com/fguillen/4732177#file-polymorphic_where_test

Rails:acts-as-taggable-on: Using tagged_with on a relation

感情迁移 提交于 2019-12-23 05:26:54
问题 Lets say I have a Model A which has a 1 to 1 association with Model B. Model B is set to 'acts_as_taggable'. I would like to return an active record relation that selects all A instances who's B attribute is tagged_with a certain string. It would look something like: A.where('b.tagged_with = ?', 'some_tag') I found this SO question, but i'm interested in getting an active record relation back so only the 2nd solution is applicable and I can't get it to work. How can I get back active record

How to query a model based on attribute of another model which belongs to the first model?

佐手、 提交于 2019-12-17 04:34:22
问题 If I have a model Person , which has_many Vehicles and each Vehicle can be of type car or motorcycle , how can I query for all persons, who have cars and all persons, who have motorcycles? I don't think these are correct: Person.joins(:vehicles).where(vehicle_type: 'auto') Person.joins(:vehicles).where(vehicle_type: 'motorcycle') 回答1: You can do as following: Person.includes(:vehicles).where(vehicles: { type: 'auto' }) Person.includes(:vehicles).where(vehicles: { type: 'motorcycle' }) Be

In Ruby, with ActiveRecord what is the best way to retrieve an object that belongs_to a parent object, given some conditions?

亡梦爱人 提交于 2019-12-11 06:51:14
问题 In my model an Organisation has_many :users , and a User has_and_belongs_to_many :roles and a Role has a name and has_and_belongs_to_many :users . In my Organisation class I have a method get_admin that is supposed to get the User belonging to that Organisation who has the Role 'admin' . Like so: def get_admin return self.users.with_role('admin') end Alas this returns an ActiveRecord::Relation object, not a User . I tried appending .first to the end of the line like so def get_admin return

Activerecord: find record by grandparent value

此生再无相见时 提交于 2019-12-07 22:07:24
问题 I have the following models: class GrandParent < ActiveRecord::Base has_many :parents has_many :children, through: :parents end class Parent < ActiveRecord::Base has_many :children belongs_to :grand_parent end class Child < ActiveRecord::Base belongs_to :parent end I'd like to find all Children where the a child's grand_parent has a value equal to TRUE, but I'm having trouble getting the syntax right. Something like: Child.where(grand_parent.value: TRUE) 回答1: You need to join the models in

Activerecord: find record by grandparent value

一笑奈何 提交于 2019-12-06 11:24:49
I have the following models: class GrandParent < ActiveRecord::Base has_many :parents has_many :children, through: :parents end class Parent < ActiveRecord::Base has_many :children belongs_to :grand_parent end class Child < ActiveRecord::Base belongs_to :parent end I'd like to find all Children where the a child's grand_parent has a value equal to TRUE, but I'm having trouble getting the syntax right. Something like: Child.where(grand_parent.value: TRUE) You need to join the models in-between to be able to reference GrandParent so you would have to join Parent first and then filter. Child

Postgres Copy from Variable with CSV data

感情迁移 提交于 2019-11-29 15:41:46
I know you can run a copy command like this from a file: "COPY zip_codes FROM '/path/to/csv/ZIP_CODES.txt' DELIMITER ',' CSV;" I'd like to copy csv data from a ruby variable so I can do like so "COPY zip_codes FROM '#{csv_data}' DELIMITER ',' CSV;" That's not possible with the SQL COPY command. COPY only copies from a file or STDIN . You can either write the content of the variable to a file or pipe it via STDIN. Only makes sense for more than a couple of rows. I think I misunderstood your question before the update, you probably don't need this: The file path can not be exchanged like other

How to query sql with active record for dates between specified times

女生的网名这么多〃 提交于 2019-11-29 03:52:52
I have a database that I want to pull only certain rows that have dates in specified ranges. I'm not sure how to do this properly in active record. Right now it looks like I'm running a standard mysql query inside of an active record query. I hope this gives you the idea of what I'm looking for. I would also like to be able to get rows with anything before today, including today and 3 days in the future. $query = $this->db->query("SELECT * FROM 'topics_list' . 'topic date' WHERE DATE(order_datetime) BETWEEN '2012-10-01' AND '2012-10-3'"); this is the way . but according to the DATE format you

Postgres Copy from Variable with CSV data

陌路散爱 提交于 2019-11-28 10:44:59
问题 I know you can run a copy command like this from a file: "COPY zip_codes FROM '/path/to/csv/ZIP_CODES.txt' DELIMITER ',' CSV;" I'd like to copy csv data from a ruby variable so I can do like so "COPY zip_codes FROM '#{csv_data}' DELIMITER ',' CSV;" 回答1: That's not possible with the SQL COPY command. COPY only copies from a file or STDIN . You can either write the content of the variable to a file or pipe it via STDIN. Only makes sense for more than a couple of rows. I think I misunderstood