ActiveModel Serializer中的自定义字段与另一个序列化程序(Custom field in ActiveModel Serializer with another serializer)
我有
has_many :sessions
User
模型。 但是对于一个特定的请求,我想根据请求设备平台(iOS,Android)仅发送一个会话。问题是我有单独的
UserSerializer
和UserSessionSerializer
,我想在UserSerializer
UserSessionSerializer
作为选项UserSerializer
给:session
字段。 喜欢这个:# UsersController render json: @user, serializer: UserSerializer, session: @session # @user's session founded by platform # UserSerializer attributes :id, :email, :username, :session def session @instance_options[:session], serializer: UserSessionSerializer end
这是不可能的,因为据我所知,我只能在
has_one
和has_many
传递序列化程序。 但我不想用has_many
渲染JSON中的所有用户会话,只创建一个。谢谢你的帮助!
I have
User
model withhas_many :sessions
. But for one particular request I want to send only one session based on request device platform (iOS, Android).The problem is that I have separate
UserSerializer
andUserSessionSerializer
and I want to passUserSessionSerializer
as option inUserSerializer
for:session
field. Like this:# UsersController render json: @user, serializer: UserSerializer, session: @session # @user's session founded by platform # UserSerializer attributes :id, :email, :username, :session def session @instance_options[:session], serializer: UserSessionSerializer end
It is impossible, because I can pass serializer only in
has_one
andhas_many
as I understand. But I don't want to render all user sessions in JSON withhas_many
, only founded one.Thanks for any help!
原文:https://stackoverflow.com/questions/49793092
最满意答案
end()的参数是一个引用传递。 就是这样,它将内部指针设置为数组的末尾。
你可以通过这样做来解决它:
$keys = array_keys($patch_array); echo end($keys);
The parameter of end() is a pass by reference. It is that way so it set the internal pointer to the end of the array.
You can fix it by doing:
$keys = array_keys($patch_array); echo end($keys);
相关问答
更多-
end()的参数是一个引用传递。 就是这样,它将内部指针设置为数组的末尾。 你可以通过这样做来解决它: $keys = array_keys($patch_array); echo end($keys); The parameter of end() is a pass by reference. It is that way so it set the internal pointer to the end of the array. You can fix it by doing: $keys = a ...
-
我测试了您上面提供的代码,但没有看到任何问题。 这让我相信文件中的数据可能有问题。 你能粘贴文件数据吗? (制表符分隔数据) 更新 - 我发现了问题 - 怀疑是这是一个数据问题(也涉及一些次要的代码更新)。 进行以下更改 - 1)打开文件时使用适当的编码,我使用utf-16。 2)在您共享的数据文件的末尾,它包含文本 - “(72413行受影响)”以及几个新行字符。 因此,您有2个选项,手动清理数据文件,或更新代码以忽略“(72413行受影响)”和“\ n”字符。 希望有所帮助。 I tested the ...
-
严格标准:非静态方法(Strict Standards: Non-static method)[2024-01-05]
如果查看DB类,那么您将看到函数Query定义为成员函数或实例方法。 在类的实例上调用成员函数。 在你的情况下,它将类似于: $db = new DB(); $db->Query($sql); 但是,您正在静态调用Query ,即DB::Query($sql) 。 不同之处在于,使用箭头->运算符在对象上调用实例变量和方法,并使用范围解析::运算符在类上访问静态函数和变量。 注意:此处使用的$sql是查询的占位符。 如果你的Query函数被定义为static function Query($sql)在DB ... -
你的数组看起来像这样: $arr = Array( 'first' => array( 'val' => 100 ), 'second' => array( 'val' => 200 ), 'thirth' => array( 'val' => 300 ), ); 您可以像这样访问它们: echo $arr['first']['val']; // 100 像这样循环他们: foreach($arr as $val){ ...
-
正如@Steven Doggart所说(并且在答案中打了我一分钟)你正在声明一个阵列,但你没有给出尺寸长度。 你有两个选择: 在声明中指定数组维度大小 或使用Redim设置数组的(维度)大小 在您的情况下,一个解决方案可能如下所示: Dim dataArray(,) As Array = New Array(3, DT.Rows.Count) {{}} 或者像这样: Dim dataArray(,) As Array = New Array(,) {{}} Redim dataArray(3, DT.Ro ...
-
postDispatch函数的签名是 public function postDispatch(Zend_Controller_Request_Abstract $request) 所以你必须指定$request参数类型: Zend_Controller_Request_Abstract final fixed solution :
Lua 2D数组错误(Lua 2D array error)[2022-10-25]
Lua中的数组从1开始,而不是从0开始。所以你的for循环必须从1开始。 Arrays in Lua start at 1, not at 0. So your for loops must begin at 1.2D数组:索引超出范围(2D arrays: Index out of range)[2024-03-06]
你使用[[AnyObject]]()实例化了一个二维数组,但它仍然是空的,这意味着你在self.internalData[0]没有任何内容可以追加。 你可以做很多事情来处理这个问题,但是如果你知道你将在self.internalData有3个数组,你可以像这样实例化每个内部数组: self.internalData = [[AnyObject](), [AnyObject](), [AnyObject]()] 现在你在self.internalData[0]上有一个空数组可以追加项目。 但是,似乎如果你已 ...这是因为indexOf方法有一个闭包参数。 它看起来像这样 let findIndex = arrayOfArrays.indexOf { (stringArray) -> Bool in return arrayToMatch == stringArray } 但它可以缩短为简单的单线程 let findIndex = arrayOfArrays.indexOf { $0 == arrayToMatch } 还要记住,返回值是可选的(未找到时返回nil)并且仅返回与匹配的collect ...PHP - 严格标准:只应通过引用传递变量[复制](PHP - Strict standards: Only variables should be passed by reference [duplicate])[2022-04-09]
查看end()签名: mixed end(array &$array) 。 它期望对数组的引用,但您提供了函数的返回值。 在严格模式下不允许这样做。 $sql = "INSERT INTO club_territories (`club_id`, `teritorije_id`) VALUES "; foreach ($params['territories'] as $key => $territory) { $sql .= "('" . $clubId . "', '" . $territor ...相关文章
更多- solr required field: id
- Solr4.7---Field、CopyField、DynamicField
- Twitter Storm 序列化
- Twitter Storm 序列化
- Solr Document [null] missing required field: id 的原因
- JSON部分序列化(二)
- 自定义Hadoop的可序列化类
- solr error logs org.apache.solr.common.SolrException: ERROR: [doc=17] unknown field alias
- Json.Net学习笔记(十三) 缩小序列化Json的大小
- ExtJS自定义组件中的事件传递问题
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)