问题标签 [has-many]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
164 浏览

ruby-on-rails - 在 ActiveRecord 中查找包含条件的帮助

我有一个Coach模型:

我想找到所有的教练,他们attribute_idnil一些,他们有一些qualifications。类似的东西。

有没有办法在一行中获取所有这些记录?

0 投票
1 回答
818 浏览

ruby-on-rails - ActiveRecord 选择了错误的 class_name

干草开发人员,

当我调用@chapter.articles时,rails 会产生以下错误:

如果我指定:class_name => "Sommaire::Article"事件,它会产生:

由于我对rails(3.0.0)比较陌生,这让我失去了!

在此先感谢您的帮助。京东


sommaire.rb中的类的代码下方

sommaire/chapter.rb中

sommaire/article.rb中

0 投票
1 回答
155 浏览

ruby-on-rails - 在 Rails 中使用 has_many 和 find() 缺少方法“%”

当我尝试访问 has_many 关系的 find 方法时,我遇到了一个非常奇怪的错误。

我在语法上做错了什么?

0 投票
2 回答
919 浏览

ruby-on-rails - 在 has_many 关联中为“无子项”创建 named_scope

我想为零帖子的博客设置一个 named_scope。

以下不起作用。

0 投票
1 回答
2173 浏览

ruby-on-rails - 如何在 form_for 中使用多个“一对多”嵌套属性

鉴于用户有很多信用卡并且信用卡有很多地址,我正在尝试创建一个表单来同时创建一个用户和信用卡的地址

相关型号代码:

控制器代码

查看代码

所以我遇到的问题是地址字段没有显示。我尝试添加@user.credit_cards.addresses.build到控制器,但出现undefined method 'build' for nil错误。

0 投票
1 回答
2438 浏览

ruby-on-rails - Rails 关联(belongs_to、has_many)无法使用创建方法(用户、帖子、评论)在表中保存 2 个 ID

试图在 rails 3 中编写一个基本的“类似博客”的应用程序,我被关联所困。我需要创建方法将 post_id 和 user_id 保存在评论表中(我需要它来检索用户编写的所有评论以显示它)

该应用程序有用户(身份验证 - 设计)、帖子(由用户发布 - 但我不确定这对我来说是否重要)和评论(在帖子上,由用户发布)。

评论表有一个 post_id,一个 body,还有一个 user_id

协会:

路线:

帖子显示视图上显示的评论帖子表单:(posts/show.html.erb)

最后,评论控制器中的 create 方法:

A.) 如果我写这个,那么 post_id 就会被写入数据库

B.)如果我写这个,一个 user_id 被写...

我试过:

但它不起作用..我怎样才能写一个保存 user_id 和 post_id 的方法?我是否还必须在评论表单中进行一些更改(例如 <% form_for [@post, @user, Comment.new] do |f| %> ?)

谢谢!

0 投票
2 回答
163 浏览

ruby-on-rails - 在 Rails 2 中,如何在 has_many 关系中添加模型时定义回调方法?

在我的项目中,我有一个包含许多主题的论坛,每个主题都有许多帖子。当我将帖子添加到主题时,我想更新主题模型中的时间戳列以记录最后一个帖子添加到主题的时间。我怎样才能做到这一点?

感谢您的关注!

0 投票
3 回答
312 浏览

ruby-on-rails - 在 Rails 中将状态值建模为关联的正确方法是什么?

我有一个名为 Contacts 的模型。

联系人可以有不同的状态“坏的、积极的、错误的……”

这些状态可能需要随着时间的推移而改变,但在所有联系人中,它们是相同的选项。

我应该这样建模吗:

然后我手动填充表中的状态类型?

然后我想使用 Ajax 单击与某个值对应的按钮来更新联系人的值。

0 投票
1 回答
6258 浏览

ruby-on-rails - 仅删除具有多个关系

我有一个:has_and_belongs_to_many :friends, :join_table => "friends_peoples".

我要添加一个朋友:@people.followers << @friend创建关系和一个新的个人资料。

现在我只想删除关系而不是个人资料。

我试过@people.friends.delete(guilty.id)了,但它删除了个人资料而不是关系。

任何的想法?

0 投票
0 回答
451 浏览

ruby-on-rails - accepts_nested_attributes and validates_uniqueness_of

The central problem: How do you merge attribute collections by a key during mass assignment from a nested form.

The details: I am using the following models:

So there is a constraint of having only one container type per location. The following views render the location and associated containers:

admin/containers/_index.html.erb

admin/containers/_form.html.erb

module Admin::ContainersHelper

Essentially, the helper adds an new container to the collections except all types have already been associated or there is already a new container in the collection. This container is preinitialized to the first not-yet-defined container type. This works out pretty well so far. Adding containers works. Deleting containers works.

The problem is: I want to achieve that choosing and adding a container type which is already in the collection should sum up their counts (instead it would violate the unique constraint). I'm not sure what would be the best way without implementing/reinventing the complete accepts_nested_attributes_for magic - actually I wanted to reduce - not increase - code and complexity by using that.