小编Alt*_*ous的帖子

Rails 3如何在没有_attributes指定的情况下允许传递嵌套属性

当使用accepts_nested_attributes_for而不是必须传递"child_attributes"时,我想传递"child".我很确定如果我在控制器中添加了很多逻辑来创建记录和子项,我可以做到这一点.但是,为了保持我的控制器清洁和逻辑应该是的位置,在这种情况下的模型,我想知道如何在执行POST或PUT时切换rails 3以使用此语法.

{
  "name": "test",
  "child_attributes": [
    {
      "id": 1,
      "name": "test_child_update"
    },
    {
      "name": "test_child_create"
    }
}
Run Code Online (Sandbox Code Playgroud)

{
  "name": "test",
  "child": [
    {
      "id": 1,
      "name": "test_child_update"
    },
    {
      "name": "test_child_create"
    }
}
Run Code Online (Sandbox Code Playgroud)

ruby activerecord ruby-on-rails ruby-on-rails-3

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

Rails 3 - 处理控制器中嵌套资源查询的最佳方法?

如果我对Rails 3了解的一件事是,如果我很难做某事,我可能做错了.所以我正在寻求帮助.

我有一些与多对多关系相关的模型.

我能够在没有问题的情况下在模型中创建关联.我的问题在于如何构建控制器以使用这些关系.如果你不知道我要去哪里,我会试着举个例子.

例如...

class Account < ActiveRecord::Base
    has_many :locations
end

class Contact < ActiveRecord::Base
    has_many :locations
end

class Location < ActiveRecord::Base
    has_and_belongs_to_many :accounts
    has_and_belongs_to_many :contacts
end
Run Code Online (Sandbox Code Playgroud)

假设我有上述型号.这将是我的资源......

resources :accounts do
    resources :locations
end

resources :contacts do
    resources :locations
end

resources :locations do
    resources :accounts
    resources :contacts
end
Run Code Online (Sandbox Code Playgroud)

所以为了缩短这一点,我想说我想要一个帐户所有位置的列表.上述路线可能是账号/ 1 /位置.因此登陆我的位置#index.

希望我在这一点上没有搞砸我的例子,但是建立这个行动的最佳方式是什么,因为它确实有多个工作......至少是帐户,联系人和所有地点的位置.

所以我最终得到这样的东西......

class LocationController < ApplicationController
    def index
        if params[:account_id]
            @locations = Location.find_all_by_account_id(params[:account_id])
        elsif params[:contact_id]
            @locations = Location.find_all_by_contact_id(params[:account_id])
        else
            @locations = Location.all
        end

        respond_with @locations
    end
end
Run Code Online (Sandbox Code Playgroud)

更新#1:澄清一下,因为我得到一些答案,表明我改变了我的模型关系.我正在使用遗留系统,在此系统中我无法改变关系.最终我的目标是清理数据库和关系,但现在我不能.所以我需要找到一个适用于此配置的解决方案.

ruby activerecord ruby-on-rails nested-resources ruby-on-rails-3

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

C#List <T> OrderBy始终返回null

这是我的设置.

public class ItemList : List<Item>
{
  public void Load() {...}
  public void Save() {...}
}
Run Code Online (Sandbox Code Playgroud)

从XML文件加载读取以填充ItemList

然后,我尝试按优先级排序项目列表.这是一个int?然而,出于测试目的,所有项目具有不同的值.

ItemList itemList = new ItemList();
itemList.Load();

ItemList newItemList = itemList
                        .OrderBy(item => item.Priority) as ItemList;

return newItemList;
Run Code Online (Sandbox Code Playgroud)

在上面的newItemList总是为null.itemList的Count为7.我进行了三次检查,并且itemList实例中的所有项都设置了优先级.

我究竟做错了什么?

我也试过......

ItemList newItemList = itemList
                        .OrderBy(item => item.Priority)
                        .ToList() as ItemList;
Run Code Online (Sandbox Code Playgroud)

似乎没有什么工作.

提前致谢!

c# generic-list sql-order-by

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

goroutines是如何工作的,当主要过程结束时它们会死吗?

我编写了一个简单的小例子,将1000万条记录插入mongodb.我开始按顺序工作.然后我查找了如何进行并发,并找到了goroutines.这看起来像我想要的,但它并不像我期望的那样表现.我实现了一个WaitGroup来阻止程序在所有goroutine完成之前退出,但我仍然遇到问题.

所以我将从正在发生的事情开始然后显示代码.当我运行没有goroutine的代码时,所有1000万条记录都插入mongodb罚款.然而,当我添加goroutine时,一些不确定的数量被输入..一般在8500左右给予或采取几百.我检查了mongodb日志,看它是否有问题,没有任何显示.所以我不确定是什么,可能是,只是没有被记录.无论如何,这是代码:

(旁注:我一次只做1条记录,但我把它拆分成一种方法,所以我可以在将来一次测试多条记录......只是还没弄明白如何用mongodb做到这一点然而.)

package main

import (
  "fmt"
  "labix.org/v2/mgo"
  "strconv"
  "time"
  "sync"
)

// structs
type Reading struct {
  Id   string
  Name string
}

var waitGroup sync.WaitGroup

// methods
func main() {
  // Setup timer
  startTime := time.Now()

  // Setup collection
  collection := getCollection("test", "readings")
  fmt.Println("collection complete: " + strconv.FormatFloat(time.Since(startTime).Seconds(), 'f', 2, 64))

  // Setup readings
  readings := prepareReadings()
  fmt.Println("readings prepared: " + strconv.FormatFloat(time.Since(startTime).Seconds(), 'f', 2, 64))

  // Insert readings
  for i := 1; i <= 1000000; i++ {
    waitGroup.Add(1) …
Run Code Online (Sandbox Code Playgroud)

go goroutine

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