我认真对待命名,所以我认为我的ActiveRecord模型的名字很难.但是,我经常提出一个名称,它与数据库或Ruby或Rails中的保留名称有一些冲突.模型或字段名称如set或group.有时问题也不会立即显现出来.我们的模型名称和字段名称列表中是否有名称的名称?
activerecord ruby-on-rails ruby-on-rails-3 active-record-query ruby-on-rails-3.2
我有这样的架构.

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
Run Code Online (Sandbox Code Playgroud)
我想获得一位经理的阅读材料.在SQL中我会做这样的事情.
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
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何在activerecord中执行此操作.我一直试图理解http://guides.rubyonrails.org/active_record_querying.html和http://guides.rubyonrails.org/association_basics.html但它并没有陷入其中.我想我只需要从一个不同的观点.
我以为我会访问这样的数据,但我想我只是不明白它是如何工作的.
managers.name
managers.stores.name
managers.stores.regions.readings.limit(10)
Run Code Online (Sandbox Code Playgroud)
我一直不得不这样这样的东西更加丑陋.
managers.first.stores.first.regions.first.readings.limit(10)
Run Code Online (Sandbox Code Playgroud) 我有一个数据库,我想只拉出某些日期在指定范围内的行.我不确定如何在活动记录中正确地做到这一点.现在看起来我正在活动记录查询中运行标准的mysql查询.我希望这可以让你了解我在寻找什么.
我希望能够在今天之前获得任何行,包括今天和未来3天.
$query = $this->db->query("SELECT * FROM 'topics_list' . 'topic date' WHERE DATE(order_datetime) BETWEEN '2012-10-01' AND '2012-10-3'");
Run Code Online (Sandbox Code Playgroud) 这是我的数据库中一个条目的一个示例:
Market id: 1, name: "Independence Park (Independently Run Farmers Market...", address: "3945 N. Springfield Ave., Chicago, IL", zipcode: "60618", created_at: "2013-01-01 21:22:24", updated_at: "2013-01-01 21:22:24"
Run Code Online (Sandbox Code Playgroud)
我想要做的就是列出我数据库中所有条目的43个zipcodes.为什么这些查询不起作用?
Market.all.each { |m| m.zipcode }Market.all.zipcode m = Market.allm.each{ |m| m.zipcode }谢谢!
我有一个客户端模型和一个产品模型,其中客户端有许多产品,而产品属于CLient.
我需要找到一个只返回客户端的查询,如果他们在Product表中有记录
客户表
id | name
--------------
1 | Company A
2 | Company B
3 | Company C
Run Code Online (Sandbox Code Playgroud)
产品表
id | name | client_id
---------------------------
1 | Product A | 1
2 | Product B | 1
3 | Product C | 3
4 | Product D | 3
5 | Product E | 1
Run Code Online (Sandbox Code Playgroud)
我只需要客户1 3
比如像
@clients = Client.where("client exists in products") #something to this effect
Run Code Online (Sandbox Code Playgroud) 如果我有一个模型Person,has_many Vehicles每个模型都Vehicle可以是类型,car或者motorcycle我如何查询所有拥有汽车的人和所有拥有摩托车的人?
我不认为这些是正确的:
Person.joins(:vehicles).where(vehicle_type: 'auto')
Person.joins(:vehicles).where(vehicle_type: 'motorcycle')
Run Code Online (Sandbox Code Playgroud) 我有类别模型和类别有很多帖子.
问题:有时在Web中的类别下不会显示过帐,即使数据库中存在记录
我通过启用config.log_level =调试并重新启动nginx乘客服务器来调查生产环境中的操作查询.现在我可以看到该类别下的记录.我无法再次重现同样的问题,很少发生.
注意:
型号如下
class Category < ActiveRecord::Base
has_many :postings, conditions: ['paid = ? AND start_date <= ? AND end_date >= ?', true, Date.current, Date.current]
end
class Posting < ActiveRecord::Base
searchkick
belongs_to :category
class << self
def payed
where paid: true
end
def activated
where :code => ""
end
def starts_on(date)
where "start_date <= ?", date
end
def ends_after(date)
where "end_date >= ?", date
end
def in_location(state,city)
where(stateid: state.id, cityid: city.id)
end
def not_deleted
where …Run Code Online (Sandbox Code Playgroud) 有这个:
class Event < ActiveRecord::Base
belongs_to :historizable, :polymorphic => true
end
user = User.create!
Run Code Online (Sandbox Code Playgroud)
我可以:
Event.create!(:historizable => user)
Run Code Online (Sandbox Code Playgroud)
但我不能:
Event.where(:historizable => user)
# Mysql2::Error: Unknown column 'events.historizable' in 'where clause'
Run Code Online (Sandbox Code Playgroud)
我必须这样做:
Event.where(:historizable_id => user.id, :historizable_type => user.class.name)
Run Code Online (Sandbox Code Playgroud)
重现问题的代码:https://gist.github.com/fguillen/4732177#file-polymorphic_where_test-rb
我有一个称为“事件”的模型,另一个名为“产品”的模型。一个事件包含许多产品,而一个产品包含许多事件(通过称为的联接模型Eventproduct)。我正在尝试设计一个查询,该查询将选择在任何情况下当前日期范围都与另一个事件的日期都不匹配的所有产品,因此,当用户创建一个具有日期范围的事件时,它将显示可用的产品,以便同一产品不能同时出现2个事件。使用活动记录查询界面可以做到这一点,还是我需要编写自己的特定SQL查询。
我的迁移看起来像:
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :make
t.string :model
t.integer :wattage
t.boolean :dmx
t.decimal :price
t.timestamps
end
end
end
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.datetime :start_date
t.datetime :end_date
t.timestamps
end
end
end
class AddContactToEvent < ActiveRecord::Migration
def change
add_column :events, :name, :string
add_column :events, :location, :string
add_column :events, :contact_number, :string
end
end
class CreateEventproducts < ActiveRecord::Migration
def change
create_table :eventproducts do |t|
t.references :product
t.references :event …Run Code Online (Sandbox Code Playgroud) 我的Gallery模型中有以下查询:
media_items.includes(:photo, :video).rank(:position_in_gallery)
Run Code Online (Sandbox Code Playgroud)
我的Gallery模型has_many Media Items,每个都有一个Photo或一个Video关联.
到目前为止这个工作正常.它返回media_items包含它们
photo或video关联的所有内容,按position_in_gallery属性排序media_item.
但是我现在有一个要求限制了该查询返回到只有那些具有的属性的照片is_processing即是nil.
是否可以进行相同的查询,但返回的照片条件相当于:
.where(photo: 'photo.is_processing IS NULL')
Run Code Online (Sandbox Code Playgroud)
请注意,无论是否包含is_processing属性,都应返回所有视频.
我试过@ mudasbwa的建议:
includes(:photo, :video).where('photos.is_processing IS NULL').rank(:position_in_gallery)
Run Code Online (Sandbox Code Playgroud)
但它让我:
错误:表"照片"缺少FROM子句条目
ruby activerecord ruby-on-rails active-record-query ruby-on-rails-4