我有3个ActiveRecord型号:
class Doc < ActiveRecord::Base
has_many :fields
has_many :forms
end
class Form < ActiveRecord::Base
belongs_to :doc
end
class Field < ActiveRecord::Base
belongs_to :doc
end
这些模型中的每个都有一个version列。在Doc上,它是最新版本;在Form和Field上,它是文档创建时的版本。查询给定表单的字段非常简单:
class Form <
我目前有一个表(post),列如下:
id, stock_code, posted_at
对于给定的股票代码SC和time T1,我可以在一段时间后检索最新的帖子,如下所示
SELECT * FROM post WHERE stock_code = SC AND time > T1 ORDER BY time asc LIMIT 1; (没有经过实际测试,但你得到了要点)
但是,我希望得到一组多个股票的结果(甚至对于表中的每一个不同的股票代码)。我可以简单地多次运行这个查询,但是这很快就会变得效率低下,最好将它合并到一个SQL查询中,但是我无法思考如何做到这一点。我希望每一行都是一个给定
第一篇文章,所以如果我没有正确遵循指导原则,我向你道歉。
我正在用Rails设计一个在线联赛管理应用程序,在数据库设计中遇到了一些问题,我宁愿现在就解决这些问题,而不是继续我已经获得的东西,然后发现它不能工作。
目前我有以下模型:
class League < ActiveRecord::Base
has_many :teams
has_many :players
end
class Team < ActiveRecord::Base
belongs_to :league
has_many :players
end
class Player &l
这可能是一个愚蠢的问题,但我想知道测试记录是否存在的好做法,即使是“belongs_to”关联?
例如:
class MyModel < ApplicationRecord
belongs_to :book
def my_method
if self.book.present? # <- Do I need this?
# Do Something
end
end
end
你们觉得怎么样?当belongs_to :book,optional: true时呢?
我有3个表,每个表都有45+列。以下是模型:
class Product < ActiveRecord::Base
belongs_to :manufacturer
belongs_to :team
end
class Manufacturer < ActiveRecord::Base
has_many :products
end
class Team < ActiveRecord::Base
has_many :products
end
我需要在某些阶段获取所有项目,因此:
@products = Product.where('products.
我有几个嵌套类,如下所示:
class Presentation < ActiveRecord::Base
has_many :campaign_records, :as => :campaignable
def campaign_records_text(joiner)
return '' if self.campaign_records.blank?
self.campaign_records.map {|c| c.to_s}.join(joiner)
end
end
class CampaignRecord < Activ
我是rails的新手,我正在使用rails多态关联,我遇到了一个问题,我有两个模型EmployeeTable和ProductTable。两者在图片模型中都有一些图片
class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
end
class EmployeeTable < ActiveRecord::Base
has_many :pictures, as: :imageable
end
class ProductTable < ActiveRecord::Base
给定的
class Post
has_many :comments
end
class Comment
belongs_to :post
end
如果我想验证一个注释总是有一个帖子,我应该使用
class Comment
belongs_to :post
validates :post, presence: true
end
或
class Comment
belongs_to :post
validates :post_id, presence: true
end
建议采用validates :post, presence: true方法,并说:
如果要确保存在
我有一些与地理相关的类,定义如下:
class Location < ActiveRecord::Base
end
class State < Location
has_many :geographies
has_many :cities
has_many :counties
end
class County < Location
belongs_to :state
has_many :geographies
has_many :cities, :through => :geographies, :uniq => true
end
我有以下几种型号:
class Todo
has_many :completions
class Project
has_many :completions
class Completion
belongs_to :todo
belongs_to :project
(所有项目都有相同的待办事项)
对于GET /projects/:id/todos上的给定项目,返回所有todo并将它们与其完成匹配的最佳方法是什么?我在考虑在控制器中迭代和设置一个虚拟属性,但是必须有一个更简单的方法吗?
谢谢!
我有点困惑,正在寻求一些建议。
我有一个用户应用程序,每个用户都有一个 company ,每个公司都有Transaction正在运行。
Company
has_many :transactions
Transaction
belongs_to :company
但是现在我需要包括类别,以便更好地组织和管理用户事务。我希望用户自己创建类别,然后将新事务添加到他直接创建的类别中。所以有点像现在的事务,belongs_to是一个类别。尽管如此,我并不想打破我目前的模式,因为它的工作方式,我也希望它,只是感觉正确(公司执行交易)。
我的路线已经嵌套够多了,所以做另一层会使我的处境更加丑陋。这
r
在一个模型上只使用belongs_to与在一个模型上使用has_many和在另一个模型上使用belongs_to有什么区别?
例如:
class Author < ActiveRecord::Base
end
class Book < ActiveRecord::Base
belongs_to :author
end
对比
class Author < ActiveRecord::Base
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :author
end
谢谢。
我有一个名为Attachment的多态模型。我正在使用gem Carrierwave保存附件。
在我的Customer编辑页面上,我执行以下代码:
puts @customer.attachments.count
@customer.attachments.each do |i|
puts i.id #outputs a blank line
end
puts @customer.attachments.count输出0。但是,迭代器仍然在附件上运行1次,并打印出一个空行来代替puts i.id。
这是我的模型:
class Attachment &l
我有两个使用people表的模型:Person和Person::Employee (从Person继承)。people表有一个type列。
还有另一个模型,Group,它有一个名为:owner的多态关联。groups表有一个owner_id列和一个owner_type列。
app/models/person.rb:
class Person < ActiveRecord::Base
has_one :group, as: :owner
end
app/models/person/employee.rb:
class Person::Employee < Person
en