我有一个Bill嵌套Customer模型的模型.该Customer模型有一个电话号码,上面有唯一性验证.在创建帐单时,我想根据电话号码获取现有记录,或者如果不存在则创建新记录.我应该怎样做RESTful?
我很难搞清楚如何在rails中序列化模型的嵌套属性.我有一个RecipeTemplate,它将已存在的Recipe存储在它的template_data属性中.Recipe有两个级别的嵌套属性.
这是在rails 3.1.0.rc4上
class RecipeTemplate < ActiveRecord::Base
serialize :template_data, Recipe
...
end
class Recipe < ActiveRecord::Base
has_many :ingredients
accepts_nested_attributes_for :ingredients
...
end
Run Code Online (Sandbox Code Playgroud)
Recipe中的成分也有嵌套属性(SubIngredients).
如果我使用如下对象设置template_data:
Recipe.includes(:ingredients => [:sub_ingredients]).find(1)
Run Code Online (Sandbox Code Playgroud)
我会得到一个TypeError"无法转储匿名类Class",这是有道理的,因为它不知道如何序列化Ingredients或SubIngredients.
如何序列化模型中的嵌套属性,以便您可以使用:
serialize :template_data, Recipe
Run Code Online (Sandbox Code Playgroud)
或者我是否必须以其他方式序列化数据并自行执行类型安全检查?
在此先感谢您的帮助
我是rails的新手所以非常感谢任何建议.
我有一个带有嵌套属性地址的类Entry,
/app/models/entry.rb
class Entry < ActiveRecord::Base
has_many :addresses, :dependent => :destroy
accepts_nested_attributes_for :addresses,
:allow_destroy => true,
:reject_if => :all_blank
end
Run Code Online (Sandbox Code Playgroud)
像这样的类地址
/app/models/address.rb
class Address < ActiveRecord::Base
belongs_to :entry
validates :zip, :presence => true
end
Run Code Online (Sandbox Code Playgroud)
我以嵌套的形式
/app/view/entries/_form.html.slim
= simple_form_for(@entry) do |f|
= f.error_notification
- @entry.addresses.build
.form-inputs
= f.simple_fields_for :addresses do |address|
= render 'address_form', :f => address
Run Code Online (Sandbox Code Playgroud)
我们的想法是,在呈现表单时,除了数据库中列出的当前地址之外,"build"还将创建一个空的"地址".保存更改后,如果创建的新地址仍为空,则会被拒绝并且不会保存到数据库中.
但是,address.rb中的验证在保存之前进行验证,因此用户无法继续执行保存操作.有什么我遗漏的吗?
我正在使用Paperclip 2.3.8运行Rails 3.0.3.我有两个模型,称它们为"Post"和"Image".Image是一个包含Paperclip图像文件的对象,用于动态地将图像添加到Posts.图片属于Post,Post有很多图片.
邮政模式:
class Post < ActiveRecord::Base
has_many :images
accepts_nested_attributes_for :images, :reject_if => lambda { |a| a[:image].blank? }, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)
图像模型:
class Image < ActiveRecord::Base
belongs_to :post
has_attached_file :image,
:styles => {
:thumb=> "100x100#",
:small => "300x300>" }
end
Run Code Online (Sandbox Code Playgroud)
我有一个表单,我想动态添加和删除图像.如果图像是正在上传的新图像,我想显示file_field; 否则,如果图像已经存在,我想显示图像和删除链接.我跟着Ryan Bates在嵌套表格上的Railscasts.动态添加图像是有效的,但是对_destory的调用没有触发.帖子包含"_destroy"=>"1",但在HTTP Post事件之后,id为7的图像仍然存在.以下是HTTP Post的摘录:
"images_attributes"=>{
"0"=>{"id"=>"7", "_destroy"=>"1"},
"1"=>{"id"=>"8", "_destroy"=>"false"},
"2"=>{"id"=>"9", "_destroy"=>"false"},
"3"=>{"id"=>"10", "_destroy"=>"false"}}
Run Code Online (Sandbox Code Playgroud)
这是表单的样子:
<%= form_for @post, :html => { :multipart => true } do |f| %>
<%= f.label :images, 'Images' %><br />
<div class="field">
<%= f.fields_for …Run Code Online (Sandbox Code Playgroud) ruby-on-rails paperclip nested-forms nested-attributes ruby-on-rails-3
我有以下型号:
class Evaluation < ActiveRecord::Base
attr_accessible :product_id, :description, :evaluation_institutions_attributes
has_many :evaluation_institutions, :dependent => :destroy
accepts_nested_attributes_for :evaluation_institutions, :reject_if => lambda { |a| a[:token].blank? }, :allow_destroy => true
validate :requires_at_least_one_institution
private
def requires_at_least_one_institution
if evaluation_institution_ids.nil? || evaluation_institution_ids.length == 0
errors.add_to_base("Please select at least one institution")
end
end
end
class EvaluationInstitution < ActiveRecord::Base
attr_accessible :evaluation_institution_departments_attributes, :institution_id
belongs_to :evaluation
has_many :evaluation_institution_departments, :dependent => :destroy
accepts_nested_attributes_for :evaluation_institution_departments, :reject_if => lambda { |a| a[:department_id].blank? }, :allow_destroy => true
validate :requires_at_least_one_department
private
def requires_at_least_one_department
if evaluation_institution_departments.nil? || …Run Code Online (Sandbox Code Playgroud) 由于无法解释的原因太长,我需要重写*_attributes=(attributes)模型的方法,该方法接受另一个模型的嵌套属性。例如,这是我的模型:
class Experience < ActiveRecord::Base
accepts_nested_attributes_for :company
def company_attributes=(attributes)
...
end
end
Run Code Online (Sandbox Code Playgroud)
首先,每次调用此方法时,我就开始创建一家新公司(用替换...为self.company = Company.new(attributes))。不用说它将去并将新公司保存到数据库中。
当我发现它正在做这种事情时,我将该方法编辑为以下内容:
def company_attributes=(attributes)
self.company.nil? ? self.company = Company.new : self.company.assign_attributes(attributes)
end
Run Code Online (Sandbox Code Playgroud)
这似乎是正确的方法(它只有在没有nil的情况下才会创建一个新对象,否则只会分配已更改的属性)。但是,对于新体验,它将新创建的公司保存到数据库中,而在编辑体验时,它将在方法的单行之后更改公司的属性,但不会将其保存到数据库中。
问题1:生成的*_attributes=(attributes)方法如何工作?
问题2:如何更改company_attributes=(attributes)方法才能实现目标:创建新体验时创建新公司,并在编辑体验时编辑公司属性。
谢谢
我有一个帖子模型has_many:照片.当用户创建新帖子时,用户也应该能够为给定的帖子选择照片.
我使用RAILS 3.2.9,nested_form,carrierwave和jquery-fileupload-rails gem和ryan bates railscasts作为指南.
所有似乎都设置正确,但问题是,当用户选择照片(触发fileupload()函数)时,会创建新的Post和新的Photo记录.一旦我按下"创建帖子",就会再次创建另一个帖子记录.
任何帮助/想法表示赞赏.
非常感谢你.
切赫
class Post < ActiveRecord::Base
has_many :photos, as: :attachable, :dependent => :destroy
accepts_nested_attributes_for :photos, :allow_destroy => true
end
class Photo < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
attr_accessible :image, :description, :post_id, :attachable_id, :attachable_type
mount_uploader :image, PhotoUploader
end
# Post Controller
def create
@post = Post.new(params[:post])
@post.save
end
# _form.html.erb
<%= nested_form_for @post, :html => { :multipart => true } do |f| %>
<%= f.fields_for :photos do |photo| %>
<% if photo.object.new_record? …Run Code Online (Sandbox Code Playgroud) nested-attributes ruby-on-rails-3 carrierwave jquery-file-upload
我有两个具有以下结构的模型:
class Wallet < ActiveRecord::Base
include ActiveModel::Validations
has_one :credit_card
accepts_nested_attributes_for :credit_card
validates :credit_card, :presence => true
validates_associated :credit_card
...
end
class CreditCard < ActiveRecord::Base
include ActiveModel::Validations
belongs_to :wallet
validates :card_number, :presence => true
validates :expiration_date, :presence => true
...
end
Run Code Online (Sandbox Code Playgroud)
我正在用RSpec测试我的应用程序的功能,我注意到一些奇怪的东西.如果我创建属性的哈希值不符合我的嵌套模型的验证标准(如具有无CARD_NUMBER),然后尝试做一个update_attributes电话,然后我得到一个无效的信用卡式返回的钱包对象嵌套模型,以及适当的错误.这是正确的预期行为.
如果我采取相同的哈希虽然并运行assign_attributes,然后save(这是所有update_attributes方法应该做的事,然后我得到返回无效钱包对象与完全无嵌套的对象.这是为什么?我怎样才能更新所有嵌套的属性值并检查错误而不保存?
我有2个型号
father.rb:
class Father < ActiveRecord::Base
has_many :sons, dependent: :destroy
end
Run Code Online (Sandbox Code Playgroud)
son.rb
class Son < ActiveRecord::Base
belongs_to :father
end
Run Code Online (Sandbox Code Playgroud)
的routes.rb
Family::Application.routes.draw do
resources :fathers do
resources :sons
end
end
Run Code Online (Sandbox Code Playgroud)
rake路线输出:
father_sons GET /fathers/:father_id/sons(.:format) sons#index
POST /fathers/:father_id/sons(.:format) sons#create
new_father_son GET /fathers/:father_id/sons/new(.:format) sons#new
edit_father_son GET /fathers/:father_id/sons/:id/edit(.:format) sons#edit
father_son GET /fathers/:father_id/sons/:id(.:format) sons#show
PATCH /fathers/:father_id/sons/:id(.:format) sons#update
PUT /fathers/:father_id/sons/:id(.:format) sons#update
DELETE /fathers/:father_id/sons/:id(.:format) sons#destroy
fathers GET /fathers(.:format) fathers#index
POST /fathers(.:format) fathers#create
new_father GET /fathers/new(.:format) fathers#new
edit_father GET /fathers/:id/edit(.:format) fathers#edit
father GET /fathers/:id(.:format) fathers#show
PATCH /fathers/:id(.:format) fathers#update …Run Code Online (Sandbox Code Playgroud) 我有以下模型结构:a Banner embeds_many Slides和eachSlide embeds_many contents
class Banner
include Mongoid::Document
embeds_many :slides
accepts_nested_attributes_for :slides, allow_destroy: true
end
class Slide
include Mongoid::Document
embedded_in :banner
embeds_many :contents
accepts_nested_attributes_for :contents, allow_destroy: true
end
class Content
include Mongoid::Document
embedded_in :slide
field :value, type: String
end
Run Code Online (Sandbox Code Playgroud)
我开始使用一张带有一些内容的幻灯片的横幅.现在我向服务器发送一个JSON请求,将新内容添加到现有幻灯片中,并创建包含其内容的新幻灯片; 就像是
'banner' => {
'_id' => '123',
'slides_attributes' => [
{
'_id' => '1',
'contents_attributes' => [
{ '_id' => '1', 'value' => 'some persisted value' },
{ 'value' => 'new content here, there is no …Run Code Online (Sandbox Code Playgroud) single-table-inheritance nested-attributes mongoid4 ruby-on-rails-4.1
validation ×2
activerecord ×1
carrierwave ×1
mongoid4 ×1
nested-forms ×1
overriding ×1
paperclip ×1
rest ×1