假设我有一个模型User
和一个序列化程序UserSerializer < ActiveModel::Serializer
,以及一个如下所示的控制器:
class UsersController < ApplicationController
respond_to :json
def index
respond_with User.all
end
end
Run Code Online (Sandbox Code Playgroud)
现在,如果我访问,/users
我将得到一个如下所示的JSON响应:
{
"users": [
{
"id": 7,
"name": "George"
},
{
"id": 8,
"name": "Dave"
}
.
.
.
]
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我想在JSON响应中包含一些与任何特定用户无关的额外信息,该怎么办?例如:
{
"time": "2014-01-06 16:52 GMT",
"url": "http://www.example.com",
"noOfUsers": 2,
"users": [
{
"id": 7,
"name": "George"
},
{
"id": 8,
"name": "Dave"
}
.
.
.
]
}
Run Code Online (Sandbox Code Playgroud)
这个例子是设计的,但它是我想要实现的很好的近似.活动模型序列化器可以实现这一点吗?(也许通过子类化ActiveModel::ArraySerializer
?我无法弄明白).如何添加额外的根元素?
我有以下型号:
class Programme < ActiveRecord::Base
has_and_belongs_to_many :nationalities, class_name: 'Nation', join_table: 'nationalities_nations'
has_and_belongs_to_many :destinations, class_name: 'Nation', join_table: 'destinations_nations'
accepts_nested_attributes_for :nationalities
accepts_nested_attributes_for :destinations
end
Run Code Online (Sandbox Code Playgroud)
和
class Nation < ActiveRecord::Base
has_and_belongs_to_many :nationality_programmes, class_name: 'Programme', join_table: 'nationalities_nations'
has_and_belongs_to_many :destination_programmes, class_name: 'Programme', join_table: 'destinations_nations'
accepts_nested_attributes_for :nationality_programmes
accepts_nested_attributes_for :destination_programmes
end
Run Code Online (Sandbox Code Playgroud)
在活动管理员中,我有以下配置,可以正确地预选任何现有的存储国家/地区参考(请参见屏幕截图).
ActiveAdmin.register Programme do
permit_params :title,
destinations_ids: [:id],
nationalities_ids: [:id]
form do |f|
f.actions
f.inputs 'Countries / Regions' do
f.input :nationalities, :as => :select, :input_html => {:multiple => true}
f.input :destinations, :as => :select, :input_html => …
Run Code Online (Sandbox Code Playgroud) 我有一个嵌套属性,我在其上执行状态验证.我正在尝试没有成功提供完整错误消息文本中返回的属性名称的翻译.
调用模型Identity
并包含一个名为identity
的模型.模型嵌套在另一个具有has_many
关系的模型中.
当前返回典型的错误消息
Identities identity can't be blank
Run Code Online (Sandbox Code Playgroud)
我想将属性(默认情况下Identities identity
)转换为其他内容.
我有
en:
activerecord:
models:
identity:
identity: "whatever"
Run Code Online (Sandbox Code Playgroud)
如果我这样做,我会收到错误
I18n::InvalidPluralizationData (translation data {:identity=>"whatever"} can not be used with :count => 1):
Run Code Online (Sandbox Code Playgroud)
我试图通过改变上面的内容来为此添加复数数据
en:
activerecord:
models:
identity:
identity:
one: "one"
other: "other"
Run Code Online (Sandbox Code Playgroud)
这会将错误更改为
I18n::InvalidPluralizationData (translation data {:identity=>{:one=>"one", :other=>"other"}} can not be used with :count => 1):
Run Code Online (Sandbox Code Playgroud)
我也试过many
而不是other
没有区别.
我已经花了几个小时试图完成这项工作,在Stack Overflow和其他地方阅读了其他问题但没有成功.为属性名称编写翻译的正确方法是什么?
我听说rails有脏/改变标志.是否可以在after_commit回调中使用它?
在我的用户模型中,我有:
after_commit :push_changes
Run Code Online (Sandbox Code Playgroud)
在def push_changes
我想知道名称字段是否更改的方法.那可能吗?
在控制台中使用自动完成功能时,我经常看到" _was
"后缀为我的属性.但我找不到任何文档或最佳实践用法.它做了什么以及如何使用它?
示例:user.fname
具有该方法user.fname_was
使用source_location,我已将其跟踪到:active_model/attribute_methods.rb",第296行,但没有任何具体内容.
如何获取ActiveRecord属性方法功能?我有这门课:
class PurchaseForm
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name,
:surname,
:email
validates_presence_of :name
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
def initialize(attributes = {}, shop_name)
if not attributes.nil?
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
Run Code Online (Sandbox Code Playgroud)
我需要做什么,有一个属性方法列出来自PurchaseForm对象的所有名称和值?
如何使用active_model_serializers序列化权限?我无法访问模型和序列化程序中current_user
的can?
方法或方法.
我无法fields_for
开始处理非ActiveRecord模型的Array属性.
蒸馏下来,我必须遵循:
车型/ parent.rb
class Parent
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
extend ActiveModel::Translation
attr_accessor :bars
end
Run Code Online (Sandbox Code Playgroud)
控制器/ parent_controller.rb
def new_parent
@parent = Parent.new
@parent.bars = ["hello", "world"]
render 'new_parent'
end
Run Code Online (Sandbox Code Playgroud)
意见/ new_parent.html.haml
= form_for @parent, :url => new_parent_path do |f|
= f.fields_for :bars, @parent.bars do |r|
= r.object.inspect
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我的页面包含["hello", "world"]
- 也就是inspect
调用分配给的数组的结果bars
.(@parent.bars
省略了fields_for
,我只是nil
显示).
如何使fields_for
AR关联成为行为 - 也就是说,对于我的bars
数组的每个成员,在块中执行一次代码?
我有一个模型的回调,需要根据表单中输入的另一个字段创建一个依赖对象.但是params
在回调方法中是未定义的.有没有其他方式来访问它?从表单传递回调方法参数的正确方法是什么?
class User < ActiveRecord::Base
attr_accessible :name
has_many :enrollments
after_create :create_enrollment_log
private
def create_enrollment_log
enrollments.create!(status: 'signed up', started: params[:sign_up_date])
end
end
Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails activemodel ruby-on-rails-3 ruby-on-rails-3.2
根据ActiveJobs指南第8节,它说:
这适用于在GlobalID :: Identification中混合的任何类,默认情况下,它已混合到Active Model类中.
Mongoid::Document
混合ActiveModel::Model
,但我GlobalID::Identification
在其included_modules中找不到.
在哪里GlobalID::Identification
定义?
我可以有效地使用任何Mongoid::Document
我的ActiveJobs吗?