我最近升级到了Rails5.2,现在我在我的一个模型中得到了一个错误,因为belongs_to方法没有被其中一个子类继承。我是不是漏掉了什么明显的东西? class B < A
belongs_to :x
belongs_to :y
end
class A < ApplicationRecord
belongs_to :z
end
Error:
D#test_should_get_new:
ActionView::Template::Error: undefined method `belongs_to' for B:Class
app/model
我正在将Rails应用程序从5.0升级到5.2,并在试图调用任何定义了关系的模型时收到此错误:
ArgumentError (A class was passed to :class_name but we are expecting a string.)
引起问题的代码
belongs_to :manufacturer,
foreign_key: :org_id_mnfr,
class_name: Organization::Manufacturer
编辑--我从原版中编辑了这段代码,以使它更容易理解。
我理解对象关系阻抗问题。我理解Rails STI和多态性( Rails方式,它不是真正的OO多态性)。我已经阅读了大量的博客和问题,但仍然找不到这个问题的答案。
class Person < ApplicationRecord (ie what was ActiveRecord::Base)
end
class Employee < Person
end
class Customer < Person
end
..。多种其他类型的人
现在,让我们说‘客户’要求扩展系统,并创建一些新的东西。让我们称之为项目,我们可以为
我对Rails3中的多态关联有一些问题。我的模型看起来像这样:
class Address < ActiveRecord::Base
belongs_to :contactable, :polymorphic => true
end
class OrganisationUnit < ActiveRecord::Base
# some other associations
end
# Subclass of OrganisationUnit
class Company < OrganisationUnit
has_one :address, :as =
我对Rails和建立联系非常陌生,所以我怀疑我遗漏了一些很明显的东西。我试图建立一个应用程序,其中一个模型有两个模型,它的has_many。第二个模型是belongs_to,第一个是第一个,has_many是第三个。第三种模式可以属于第一种模式,也可以属于第二种模式。
具体来说,我有一个墙上的模型,里面有图片和拼贴画。这面墙既可以容纳图片,也可以容纳拼贴,也不能容纳。拼贴纸可以保存图片。
class Wall < ActiveRecord::Base
belongs_to :user
has_many :collages
has_many :pictures
end
class C
我正在制作一个Rails 4应用程序。每个incident都属于一个user。每个事件的has many events。我希望我可以用current_user.incidents.new创建一个事件,并传递给它一个存在于事件模型中的属性message。在创建时,我希望使用上述message创建一个新事件。
这是我的事故模型。
class Incident < ActiveRecord::Base
# The 'incident' model.
# Incidents are created by Users (belongs_to :user)
# Incid
使用Ruby on Rails,我如何实现多态has_many关系,其中所有者始终是已知的,但关联中的项将是由所有者中的列指定的某种多态(但同质)类型?例如,假设Producer类has_many products但producer实例实际上可能有许多自行车、冰棒或鞋带。我可以轻松地获得每个产品类别(自行车、冰棒等)与Producer有belongs_to关系,但给定一个producer实例,如果产品的类型不同(每个producer实例),我如何获取产品集合?
Rails多态关联允许生产者属于许多产品,但我需要相反的关系。例如:
class Bicycle < ActiveRecord:
我试图拥有一个其他模型继承的父模型,以及一个与第一个模型中的子类有关联的二级类。到目前为止..。
我使用第一个命令创建父模型。
rails g model Person name:string age:integer height:float
然后,我将Person设置为父类,并将继承自它的两个子类设置为如下所示。
class Person < ActiveRecord::Base
self.abstract_class = true
end
class Driver < Person
self.table_name = "Driver"
这不应该太难,但我找不到任何答案。尝试将我的模型组织在文件夹中,并使用rails和mongoid嵌入文档。
我的文件夹结构:
app/models/book.rb Book
app/models/book/author.rb Book::Author
app/models/book/author/bio.rb Book::Author::Bio
app/models/book/author/interest.rb Book::Author::Interest
我的模型课:
class Book
include Mongoid::Document
include Mo
class CashOrderStatus < ActiveRecord::Base
belongs_to:cash_order
end
通常,db需要一个表cash_order_statuses来映射这个模型,但现在我想
将此模型映射到特定的sql视图,如
select * from order_statues where cash_order_id is not null <=> CashOrderStatus
rails是否提供了一些方法来实现这一点
我正在用Ruby on Rails编写一个模型,使用带有mongoid的MongoDB。我有三个模型:用户,商店,评论。评论属于用户和商店。
class Review
include Mongoid::Document
field :rating, type: Float
field :body, type: String
belongs_to :user
belongs_to :store
def self.is_unique
where(user: user, store: store).exists?
end
end
我正在尝试通过查看评论是否
这里的Rails初学者:
我已经有了一个数据库和表,所以命名约定让我头疼。
class Item < ActiveRecord::Base
belongs_to :categorie, :foreign_key => "catid"
end
class Categorie < ActiveRecord ...
has_many :item
end
i = Item.first # Ok
c = i.Categorie # Ok, finds proper Categorie based on "catid
我想创建一个同时使用MongoDB和MySQL的应用程序。具体来说,我希望mongodb存储所有用户的评论,而MySQL将存储用户模型。
class User < ActiveRecord::Base
has_many :comments
end
class Comment
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :user
end
嗯,一切看起来都很好,除了当我去rails控制台运行这个。
k = Comment.new
k.user = User.first
我得到了
NoM
我在app / model /user.rb中有一个带有用户模型的应用程序。
现在我编写了一个引擎,它需要在user类中使用额外的方法。
class User < ActiveRecord::Base
# some code
end
当我使用其他方法将用户模型添加到供应商/plugins/foo/app/ model /user.rb时,这些方法是未定义的,rails找不到它们。
class User < ActiveRecord::Base
# some additional code
end
当我添加从User继承的foo_user模型时,它可以工作,但这不是我想要的
这里是Rails新手,正在尝试让一个新的控制器工作。
当我试图显示一个现有的实例时,我在一个辅助方法上得到了一个未定义的方法错误。代码如下。知道为什么getRecipes是未定义的吗?!
控制器:
def show
id = params[:id]
recipe_ids = ConcreteMenu.getRecipes(id)
respond_to do |format|
format.html
end
end
模型
require 'json/objects'
class Concr
我正在研究Rails 4应用程序,最近遇到了一个奇怪的问题。我在这里寻求你的帮助。敬请指教。
为了理解问题,创建了一个小的gist片段。
我只想总结一下:
# app/models
class User < ActiveRecord::Base
has_many :responses, dependent: :destroy
end
class Response < ActiveRecord::Base
has_one :report
has_many :points
belongs_to :user
end
class Report < ActiveRe