多亏了rails的autosave特性,我知道关联是自动保存的。
有时,我会遇到这样的情况:某些关联的模型没有保存,但父模型却保存了,即使我是在事务中保存的。
class Conversation
has_many :participations
has_many :messages
end
class Participation
belongs_to :conversation
end
class Message
belongs_to :conversation
belongs_to :participation
end
conversation = Conver
我们有三节课
class a
has_many :b
has_many :c, through: b
end
class b
belongs_to :a
belongs_to :c
end
class c
has_many :b
has_many :a, through: :b
validates_uniqueness_of :name
end
当我尝试创建类c的实例时,如果数据库中有两个类a的对象具有相同的name值,我惊讶地发现类c上的验证正在运行!当我根本不应该接触类c行时,为什么还要运行验证呢?
@max要求举个例子。
创建新的Rails 5项目后,请执
一切正常工作,直到我升级到Rails 3.2.11。
我的模型就是这样设置的:
class Student < ActiveRecord::Base
has_many :institutes
has_many :teachers, :through => :institutes
end
class Teacher < ActiveRecord::Base
has_many :institutes
has_many :students, :through => :institutes
end
class Institute < A
我目前正在尝试实现我的第一个非平凡的Rails应用程序,并且在实现设计时遇到了一些问题。最初,我使用的是一种手动的身份验证方法,如Hartl Rails教程中所描述的。在退出(或者说我相信)所有的手工代码之后,我添加了Devise,以及一些自定义字段,所有这些字段在封面下都能很好地工作,这意味着我可以注册、登录,或者以其他方式使用这个应用程序。
然而,我现在遇到的问题是,我忽略了验证中的一个技巧,因为一旦我导航到用户注册视图,表单就会呈现完整的大、难看、红色错误消息,如下所示:
Email can't be blank
Password can't be blank
First
我正在将Rails应用程序移植到Rails 4.2。这个Rails应用程序在关联中包含了一些相当复杂的手动SQL代码--部分原因是DB优化(例如子选择而不是联接),部分原因是在编写Rails 3.0时没有可行的替代方法(Rails 3.0),一定程度上是由于缺乏知识(我希望至少这将是很容易解决的)。
示例:一个InternalMessage类。消息可以在用户之间发送( InternalMessage的接收方和消息的“删除”存储在InternalMessagesRecipients中,因为可以有多条消息),它们可以被读取、回复、转发和删除。该协会的情况如下:
class User < AR
我有个新手rails的问题。我正试图通过HABTM关系来确保一个模型至少有一个关联。基本上,我创建了以下验证:
validate :has_tags?
def has_tags?
errors.add(:base, 'Must have at least one tag.') if self.tags.blank?
end
当我创建一个新的记录时,这很好。问题是,当我取下模型并试图删除关联时,就会这样做:
tag = Tag.find(params[:tag_id])$
@command.tags.delete(tag)$
这是允许的,即协会将被删除。基于我对HABTM
对于一个新项目,我的公司正在考虑使用亚马逊SimpleDB来简化数据存储。该应用程序是一个简单的web仪表板,将使用Ruby on Rails创建。不过,我想知道的是,在使用SimpleDB作为后端的同时,我是否仍然可以使用像has_many和belongs_to这样的关联。
例如,应用程序具有登录并具有消息的用户。在使用MySQL的“普通”Rails中,我可以通过内置的关联很容易地做到这一点。使用SimpleDB作为存储引擎会阻止我这样做吗?另外,我需要重新实现登录功能吗?我最初打算使用restful_authentication插件,但我不知道是否必须重写它才能使用SimpleDB。
我遇到了一个has_many关联的问题,这个关联应该在所有孩子都是安定状态时才能验证。
设置
rails g scaffold Hotel name
rails g scaffold RoomCategory name hotel:references
app/models/hotel.rb
class Hotel < ActiveRecord::Base
has_many :room_categories, dependent: :destroy
accepts_nested_attributes_for :room_categories,
我正在开发学校的ERP系统。开发人员登录用户创建admin (机构)和admin (机构)创建(教师、学生)。
我想为每个机构生成单独的网址(这有他们自己的登录),每个机构有三个角色(管理员,教师,学生)。
class Institute
has_many :institute_admins
has_many :students
has_many :teacher
end
class InstituteAdmin
belongs_to :institute
end
class Student
belongs_to :institute
end
cla
在Rails 4中,确保存在关联的最佳方法是什么?看起来您可以使用外键或关联变量本身进行测试。我对Rails还很陌生,所以这可能是个愚蠢的问题。我还认为,以下两种方法都可能有效。
# method 1:
class Region < ActiveRecord::Base
# In order to validate associated records whose presence is required,
# you must specify the :inverse_of option for the association:
has_many :flags, inv
嗨,我正在创建3模型与红宝石rails,但我有一些问题。这是我的模型代码
class Company < ActiveRecord::Base
has_many :pendings
has_many :products, :through => :pendings
end
class Product < ActiveRecord::Base
has_many :pendings
has_many :companies, :through => :pendings
end
class Pending < ActiveRecord::Bas
从Rails 3.2升级到4.1之后,下面的代码现在失败了:
在控制器/规范中:
post = user.posts.build
post.contacts << contact # contact is a persisted record
post.save! # now fails
我基本上是想保存帖子和它的相关联系人,这应该是创建一个contact_publishment记录的动态。错误出现在新的contact_publishment记录上:“可发布不能为空白”
模式:
class Contact
...
has_many :contact_publishments
我有一个rails 5项目,我从一月份开始就在做这个项目,今天我注意到我可以在不指定父记录的情况下创建子记录。这相当令人担忧,因为我验证了我使用的是rails 5.1.2和ruby 2.4.1。该网站仍在开发中。我打开了rails控制台,并测试了我的一些其他模型,它们应该有父验证(在rails 5中应该是默认的验证),这些记录也是在没有关联的情况下创建的。
这没有任何意义。我确实知道这在某种程度上是有效的。最大的变化发生在本周早些时候,我在gem中添加了用于文件上传的“回形针”。对于要检查的内容,有人有什么建议或想法吗?
class Order < ApplicationRecord
我有这些模型,但是当我做一个Message.last.people或Message.last.recipient_lists时,我会得到一个错误。如何引用带有活动记录的邮件的收件人列表或人员?我需要做RecipientList.where(:message => Message.last)吗?似乎应该有更好的方法通过.message来做到这一点吗?
class Message < ActiveRecord::Base
has_many :people, through: :recipient_list
end
class Person < ActiveRecor
我的数据模型:
集合属于用户。
收藏有很多艺术。
艺术属于收藏品。
Rails模型:
class User < ApplicationRecord
end
class Collection < ApplicationRecord
has_many :arts
belongs_to :user
end
class Art < ApplicationRecord
belongs_to :collection
end
问题代码:
def index
@arts = Art.where(:user => current_user)
end
我通过has_many,belongs_to协会将这4个模型连接在一起。基本的是2(配方,成分)。另外两个存储基于前两个的附加信息。
class Recipe
has_many :ingredients, :through => :prescriptions
has_many :prescriptions
end
class Ingredient
has_many :recipes, :through => :prescriptions
has_many :prescriptions
has_many :stocks
end
class Stock
be
我有一个包含User模型类的users_manager引擎。
在另一个购物引擎中,我使用shopping/lib/shopping.rb中的以下代码在User模型中添加了一些关联
module Shopping
class Engine<Rails::Engine
initializer :shopping_append_user do
UsersManager::User.class_eval do
has_many :products,:class_name=>"Shopping::Product"
ha