我是Rails新手,试图创建一个简单的Rails4应用程序。我有设计创业板生成的用户模型,考试模型和参与模型都是由脚手架生成器生成的。
这些是我的模型:
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
end
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
class
我正在构建一个Rails应用程序,其中有以下实体:用户、位置和评审。我想让每个用户只为一个地方添加一个评论。我想知道什么是最好的地方来进行这种验证-控制器还是模型?
这就是现在的样子:
审查(模式):
class Review < ActiveRecord::Base
belongs_to :user
belongs_to :place
...
地点(模型):
class Place < ActiveRecord::Base
has_many :reviews
...
用户(模型):
class User < ActiveRecord::Base
ha
我的rails 5应用程序中有两个模型:
class Post < ApplicationRecord
has_many :tags
end
class Tag < ApplicationRecord
belongs_to :post
end
例如,在我的数据库中,我有两个帖子,每个帖子有两个不同的标签。如何通过两个特定的标记(tag.title="tagname1“和tag.title=”tagname2“)搜索我的帖子:
Post.includes(:tags).where(tag: {title: "tagName1"}).where(tag
我有一个用户模型:
class User < ActiveRecord::Base
has_many :products
end
和一个产品模型:
class Product < ActiveRecord::Base
belongs_to :user
end
Product有一个名为product_id的列,它是alpha数字的,我希望用户可以拥有许多产品,但在product_id上应该是唯一的。我可以在product_id上添加一个索引为unique,但这意味着另一个用户不能拥有类似的产品。
为了简化这个问题,应该在每个product_id. user_id 和中都
通过与表Foo、FooBar和has_many关联,我实现了Bar::。下面是它们的实现: Class Foo < ApplicationRecord
has_many :foo_bars, dependent: :destroy
has_many :bars, through: :foo_bars
end
Class FooBar < ApplicationRecord
belongs_to :foo
belongs_to :bar
end
Class Bar < ApplicationRecord
has_many :foo_bars, depe
当存在多个进程时,在Rails中使用唯一性验证是不安全的,除非约束也在数据库上强制执行(在我的示例中是PostgreSQL数据库,请参见)。
在我的例子中,唯一性验证是有条件的:只有当模型中的另一个属性变为真时,它才会被强制执行。所以我有
class Model < ActiveRecord::Base
validates_uniqueness_of :text, if: :is_published?
def is_published?
self.is_published
end
end
因此,模型有两个属性:is_published (布尔值)和text (文
我有如下的模型关联:
class Group < ActiveRecord::Base
has_many :group_links, :dependent => :destroy
end
class GroupLink < ActiveRecord::Base
belongs_to :group
validates_presence_of :group_id
validates_presence_of :url, :message => "We need a url to
多亏了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
我有一个父母模型(鸡尾酒)和一个孩子(剂量)。 我有一个嵌套的表单,在其中我希望能够在旅途中创建/删除剂量对象。 创建部分在强参数中没有_destroy属性就可以工作,但是当我将_destroy添加到属性散列以便能够删除时,我得到了错误unknown attribute '_destroy' for Dose。我不确定我错在哪里。 另外,我认为我的代码过于复杂了,应该有一种比我正在做的更简单的方法来创建剂量( add_doses方法有必要吗?)。如果我能得到一些反馈,我将不胜感激。 鸡尾酒: class Cocktail < ApplicationRecord
va
有没有一种方法可以保存对象,但如果给定字段的validation_uniqueness_of失败,则返回该对象?例如,我有以下内容:
class User
has_many :words
...
end
class Word
belongs_to :user
validates_uniqueness_of :title
...
end
在这种情况下,如果用户已经保存了word,我想返回Word对象,或者返回新保存的word对象。我发现自己正在通过自己的搜索检查单词object是否存在,如果不存在,则执行保存:
existing_word = find_by_word_and_
我有一个rails模型来验证order_number值的唯一性,所以应该从1_000_000开始,所以我添加了一个变量,用作第一个值:
# order model
STARTING_NUMBER = 1_000_000
validates :order_number, uniqueness: true
当我通过Rubocop检查我的代码时,我出错了:
app/models/order.rb:3:3: C: Rails/UniqueValidationWithoutIndex: Uniqueness validation should be with a unique index.
va
我有一个博客应用程序,用户可以发布文章,其他用户可以评论文章,评论可以投票表决。在任何投票系统中,用户对评论进行一次投票,如果愿意的话,可以在帖子上对所有评论进行投票。但在这个问题上,我希望限制用户对一篇文章的一项评论投一票,这样评论就可以根据用户投票进行排名。因此,唯一性验证检查用户是否对文章的评论进行了投票。
class User < ActiveRecord::Base
has_many :articles
end
class Article < ActiveRecord::Base
belongs_to :user
has_many :c
这些是我的模特
class User < ActiveRecord::Base
has_many :memberships
has_many :groups, through: :memberships
end
class Group < ActiveRecord::Base
has_many : memberships
has_many :users, through: :memberships
belongs_to :group_type
end
class Membership < ActiveRecord::Base
belongs_to
在我的Rails 4应用程序中,有五种模型:
class User < ActiveRecord::Base
has_many :administrations
has_many :calendars, through: :administrations
end
class Calendar < ActiveRecord::Base
has_many :administrations
has_many :users, through: :administrations
has_many :posts
end
class Administration <
从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