active-record-query

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

眉间皱痕 提交于 2019-11-27 22:17:02
问题 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

Reserved names with ActiveRecord models

心已入冬 提交于 2019-11-27 20:21:50
I take naming pretty seriously, so I think pretty hard about good names for my ActiveRecord models. However, I frequently come up with a name and it has some conflict with a reserved name, either in the database or Ruby or Rails. Model or field names like set or group . Sometimes the issue isn't immediately apparent either. Is there a list somewhere of names we can't use in our model names and a list for field names? Here's a couple of resources to get you started: Rails reserved words . This one seems to be fairly up to date, last updated on Feb 2010. It even goes beyond rails and lists some

ActiveRecord query through multiple joins

早过忘川 提交于 2019-11-27 18:34:22
I have a schema like this. managers has_many :emails has_many :stores emails belongs_to :manager stores belongs_to :manager belongs_to :region regions has_many :stores has_many :readings readings belongs_to :regions I want to get readings for a manager. In SQL I would do something like this. SELECT * FROM managers JOIN stores ON stores.manager_id = managers.id JOIN regions ON stores.region_id = regions.id JOIN readings ON readings.region_number = regions.number WHERE manager.name = 'John Smith' AND regions.number = '1234567' LIMIT 100 I can't figure out how to do this in activerecord. I have

Reserved names with ActiveRecord models

非 Y 不嫁゛ 提交于 2019-11-26 22:55:45
问题 I take naming pretty seriously, so I think pretty hard about good names for my ActiveRecord models. However, I frequently come up with a name and it has some conflict with a reserved name, either in the database or Ruby or Rails. Model or field names like set or group . Sometimes the issue isn't immediately apparent either. Is there a list somewhere of names we can't use in our model names and a list for field names? 回答1: Here's a couple of resources to get you started: Rails reserved words.

ActiveRecord query through multiple joins

你。 提交于 2019-11-26 19:29:45
问题 I have a schema like this. managers has_many :emails has_many :stores emails belongs_to :manager stores belongs_to :manager belongs_to :region regions has_many :stores has_many :readings readings belongs_to :regions I want to get readings for a manager. In SQL I would do something like this. SELECT * FROM managers JOIN stores ON stores.manager_id = managers.id JOIN regions ON stores.region_id = regions.id JOIN readings ON readings.region_number = regions.number WHERE manager.name = 'John

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

两盒软妹~` 提交于 2019-11-26 19:07:16
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') You can do as following: Person.includes(:vehicles).where(vehicles: { type: 'auto' }) Person.includes(:vehicles).where(vehicles: { type: 'motorcycle' }) Be carefull with .joins and .includes : # consider these models Post # table name is posts belongs_to :user #^^