首页 \ 问答 \ Rails嵌套关联和重新编号(Rails nested association and restarted numbering)

Rails嵌套关联和重新编号(Rails nested association and restarted numbering)

如果我有这样的嵌套资源:

resources :users
  resources :posts
end

和一个user has_many posts ,有可能让Rails根据URL中的父关联开始编号? 例如,目前,嵌套资源仅抓取ID:

@user.posts.find(params[:id])

这正确地命名空间的帖子,只允许来自@user帖子...但是,有没有办法让post_id是独立的? IE我希望每个用户的帖子从1开始,其中:

/users/1/posts/1
/users/2/posts/1

其实指两个不同的职位?


If I have a nested resource like so:

resources :users
  resources :posts
end

and a user has_many posts, it is possible to have Rails start numbering based on the parent association in the URL? For example, currently, nesting resources just grabs the ID:

@user.posts.find(params[:id])

This correctly namespaces the posts, only allowing posts from @user... however, is there a way such that the post_id is independent? I.E. I want each user's posts to start at 1, where:

/users/1/posts/1
/users/2/posts/1

Actually refer to two different posts?


原文:https://stackoverflow.com/questions/8752120
更新时间:2021-12-08 15:12

最满意答案

要获得记录1,您可以使用Model.IEThreadpost.OrderBy(x => x.Entity_ID_HERE).First()

使用foreach跳过一个并得到其余的。

 @foreach (var item in Model.IEThreadpost.OrderBy(x => x.Entity_ID_HERE).Skip(1).Take(9)) //i think skip and take needs ordering
  {

  }

To get record 1 you could use Model.IEThreadpost.OrderBy(x => x.Entity_ID_HERE).First()

to use foreach to skip one and get the rest.

 @foreach (var item in Model.IEThreadpost.OrderBy(x => x.Entity_ID_HERE).Skip(1).Take(9)) //i think skip and take needs ordering
  {

  }

相关问答

更多
  • 您在此处的查询中选择它: SELECT node.name, (COUNT(parent.name) - 1) AS depth 因此,在您的foreach中,您需要检查所需的密钥,或检查您不想要的密钥。 foreach($iterator as $key=>$value) { if ($key != 'name') { continue; } echo $key.' -- '.$value.'
    '; } 作为后者的一个例子。 如果你想要 ...
  • 首先,完全可以这样做。 其次,你不应该这样做。 考虑使用
    使布局“流动”。 使用div创建的“表”将随窗口调整大小。 @{ int groupings = 3; var grouped = Model.Select((x,i) => new { x, i = i / groupings }) .GroupBy(x => x.i, x => x.x); } < ...
  • 我建议使用像Jammit这样的资产包装器,它可以根据需要将多个脚本缩减为一个压缩脚本。 通过这种方式,您可以构建脚本,但是对您来说更有意义,将它们分成您希望的任意数量的文件,资产句柄将负责压缩和组合它们。 如果某些脚本太大而无法在任何地方加载,则始终可以将更大的脚本加载到需要使用content_for脚本的视图中 例如: <% content_for :head do %> <%= include_javascripts :my_big_files %> <% end %> I recommen ...
  • 只需为每行三个结果添加一些基本逻辑: echo '
    '; $count = 0; foreach ($profiles as $p) { echo '
    ' . $p->name . '
    '; $count++; if($count%3==0){ print '
    '; $count = 0; } } echo '
  • 虽然我不是100%肯定你想要根据你的问题完成什么......我猜你在想如何在循环中迭代Student的IRegister ... public interface IRegister { string FirstName { get; } string MiddleName { get; } string LastName { get; } string EmailAddress { get; } List Student { ge ...
  • 不要做for循环。 只需遍历数据库结果,并使用in_array来测试当前类别是否属于所选类别之一。 while ($row = mysql_fetch_array($result10)) { if (in_array($row['an_location'], $var)) { $ka = 'selected="selected"'; } else { $ka = ''; } $tree .= "
  • 要获得记录1,您可以使用Model.IEThreadpost.OrderBy(x => x.Entity_ID_HERE).First() 使用foreach跳过一个并得到其余的。 @foreach (var item in Model.IEThreadpost.OrderBy(x => x.Entity_ID_HERE).Skip(1).Take(9)) //i think skip and take needs ordering { } To get record 1 you could ...
  • 由于JDK 8尚未发布,因此很难在此时考虑最佳实践。 但是,基于早期使用这些API,有一些有趣的观察结果。 forEach()方法现在是Iterable ,它由Collection继承,因此所有集合都可以使用forEach() 。 可以使用Arrays.asList()将数组包装在集合中,也可以使用Arrays.stream()将数据包装在流中。 这些只是包装纸; 他们不会将所有元素复制到新容器中。 关于性能, Iterable.forEach(action)的默认实现只是通常的“增强型for循环”,它创建 ...
  • 尝试在循环之后关闭连接(并且,就此而言,您可以在循环之前准备查询): $getSub = $con->prepare("SELECT `sectionID`, `sectionName` FROM `sections` WHERE `sectionID`=? AND `sectionPerms`=1"); foreach($subChild as $sub) { $getSub->execute(array($sub)); $data = $getSub->fetch(PDO::FETCH ...
  • 应该读 ^ 或者你可以使用 ^ 这将自动HTML.Encode为您的值。 should read ^ or you can ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。