标签: nested-attributes

SQL子查询COUNT CASE

如果嵌套子查询的计数为0,我想要SELECT查询返回特定值...

SELECT
  ( SELECT (CASE COUNT(*) = 0 THEN 'TRUE' ELSE 'FALSE' END)
    FROM List
    WHERE Status = 1
      AND Deleted = 1
  ) AS Status
Run Code Online (Sandbox Code Playgroud)

这不起作用这个语法有什么问题?

sql t-sql conditional nested-attributes

1
推荐指数
1
解决办法
5770
查看次数

如何访问嵌套属性的验证错误?

<%= form_for @article , :html => {:multipart=> true} do |f| %>
  <% if @article.errors.any? %>
    <ul>
      <% @article.errors.full_messages.each do |msg| %>
       <li><%= msg %></li>
      <% end %>
    </ul>
  <% end %>
Run Code Online (Sandbox Code Playgroud)

上面是我的表单的一个片段,我可以访问文章的验证,即:author, :title 的validates_presence 但是我无法访问我为nested_attributes 设置的验证,这些验证恰好是照片。关于如何显示错误消息的任何想法?

ruby-on-rails nested-attributes nested-form-for

1
推荐指数
1
解决办法
4300
查看次数

在嵌套属性上没有将符号隐式转换为整数

我在编辑嵌套属性时遇到问题。我收到此错误:

no implicit conversion of Symbol into Integer
Run Code Online (Sandbox Code Playgroud)

事件.rb:

Class Event < ActiveRecord::Base
  has_many :event_joins, :dependent => :destroy
  accepts_nested_attributes_for :event_joins
end
Run Code Online (Sandbox Code Playgroud)

events_controller.rb :

private
   def event_params
     params.require(:event).permit(event_joins_attributes: [:duration]) 
   end
Run Code Online (Sandbox Code Playgroud)

_form.html.erb :

 =f.fields_for :event_joins_attributes do |a|
    =number_field_tag 'event[event_joins_attributes][duration]'
 end
Run Code Online (Sandbox Code Playgroud)

如果我改变了我params之前的许可

params[:event][:event_joins_attributes][:duration] = params[:event][:event_joins_attributes][:duration].to_i
Run Code Online (Sandbox Code Playgroud)

我有以下错误:

no implicit conversion of String into Integer
Run Code Online (Sandbox Code Playgroud)

我已经阅读了很多关于大规模分配的嵌套属性的帖子,但没有任何效果。这是我读过的部分帖子。

强参数允许所有属性用于嵌套属性

rails-4-strong-parameters-nested-objects

白名单属性

当然,我不想做

params.require(:event).permit!
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-attributes strong-parameters ruby-on-rails-4

1
推荐指数
1
解决办法
4929
查看次数

为什么 Rails 5 不保存嵌套属性,因为父模型不首先保存

我正在使用 Rails 5 及其最新稳定版本的所有内容。所以我得到以下信息:

\n\n
\n

您已将关联设置为必需,但它丢失了。\n 在 Rails 5 中,默认情况下将关联设置为必需,因此,如果您想要\n 将其保留为空,则需要在\n 模式下对关联设置可选项:true

\n
\n\n

这太棒了,我明白发生了什么,但是在我的一生中,我无法弄清楚如何让父模型首先保存,因此 user_id 被转换为嵌套模型记录。我在上面到处都看到了相同的答案,但是除了将初始化程序中的默认值从 true 更改为 false 之外,没有人解释解决方法。这并不能解决问题,因为记录确实保存了,但不包含 user_id。

\n\n

下面是我的代码库,我想问而不是用上面的引用来回应,有人可以启发我如何在保存时将 USER_ID 字段放入嵌套属性中。我拒绝禁用验证并手动处理插入,因为这不是 ruby​​ 方式并且违反了标准!\n提前感谢任何可以直接回答这个问题并且没有偏离 ruby​​ 方式的模糊解释的人!

\n\n
###Models\n#Users\nclass User < ApplicationRecord\n  has_one :profile, inverse_of: :user\n  accepts_nested_attributes_for :profile, allow_destroy: true\nend\n\n#Profiles\nclass Profile < ApplicationRecord\n  belongs_to :user, inverse_of: :profile\nend\n\n###Controller\nclass UsersController < ApplicationController\n  before_action :set_user, only: [:show, :edit, :update, :destroy]\n\n  # GET /users\n  # GET /users.json\n  def index\n    @users = User.all\n  end\n\n  # GET /users/1\n  # GET /users/1.json\n  def …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails nested-forms nested-attributes ruby-on-rails-5

1
推荐指数
1
解决办法
4502
查看次数

Rails 4 嵌套属性和 sti 模型 update_attributes 创建新记录

我有一个使用页面模型运行的 rails 4 实例,该模型具有不同类型的页面内容,例如继承自 PageContent 的 PageContent::Text。

在我的表单中,我呈现了我所有的 @page.page_contents,正如您在表单片段中看到的那样。

如果我更新我的页面,记录不会更新,它们会被创建为新记录。

# update controller
class Admin::PagesController < Admin::BaseController

  def update
    @page = Page.find_by_url(params[:id])

    respond_to do |format|
      if @page.update_attributes(page_params(@page.type.parameterize.gsub('page','')))
        format.html { redirect_to edit_admin_page_path(@page) }
      else
        format.html { render :action => "edit" }
      end
    end
  end

  def page_params(type)
    params.require("#{type}_page".to_sym).permit(:name, :contents_attributes => [:body, :type])
  end

end

# content model
class PageContent::Text < PageContent
end

# page model
class Page < ActiveRecord::Base
  has_many :contents, class_name: 'PageContent', dependent: :destroy
  accepts_nested_attributes_for :contents
end

# form snippet
<textarea …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails sti nested-attributes

0
推荐指数
1
解决办法
1311
查看次数

Rails:fields_for无效

我下面这个RailsCastNested Model Form的事,但似乎不可思议.

这是Model关系

class Question < ActiveRecord::Base
  belongs_to :survey
end

class Survey < ActiveRecord::Base
    has_many :questions
    accepts_nested_attributes_for :questions
end
Run Code Online (Sandbox Code Playgroud)

_form.html.erb(由创建者scaffold)

<div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %> 
  </div>
   <% f.fields_for :questions do |builder| %>
    <div class ='question'>
      <%= builder.label :content, "Question" %>
      <br>
      <%= builder.text_area :content, :rows => 3 %> 
    </div>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

survey_controller.rb

# GET /surveys/new …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-attributes

-1
推荐指数
1
解决办法
402
查看次数