小编Jes*_*per的帖子

活动模型序列化程序belongs_to

这个问题与AMS 0.8有关

我有两个型号:

class Subject < ActiveRecord::Base
  has_many :user_combinations
  has_ancestry
end

class UserCombination < ActiveRecord::Base
  belongs_to :stage
  belongs_to :subject
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

两个序列化器:

class UserCombinationSerializer < ActiveModel::Serializer
      attributes :id
      belongs_to :stage
      belongs_to :subject
end

class SubjectSerializer < ActiveModel::Serializer
  attributes :id, :name, :description, :subjects

  def include_subjects?
    object.is_root?
  end

  def subjects
    object.subtree
  end
end
Run Code Online (Sandbox Code Playgroud)

UserCombination序列化时,我想嵌入整个主题子树.

当我尝试使用此设置时,我收到此错误:

undefined method `belongs_to' for UserCombinationSerializer:Class
Run Code Online (Sandbox Code Playgroud)

我试过改变UserCombinationSerializer这个:

class UserCombinationSerializer < ActiveModel::Serializer
  attributes :id, :subject, :stage
end
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我没有错误,但是subject以错误的方式序列化 - 不使用SubjectSerializer. …

ruby-on-rails active-model-serializers

26
推荐指数
2
解决办法
2万
查看次数

AssociationTypeMismatch用于相同的模型

摘要/错误

我在我的应用程序中的不同位置出现此错误:

ActiveRecord::AssociationTypeMismatch in Settings::CompaniesController#show

Company(#70257861502120) expected, got Company(#70257861787700)

activerecord (3.2.11) lib/active_record/associations/association.rb:204:in `raise_on_type_mismatch'
activerecord (3.2.11) lib/active_record/associations/belongs_to_association.rb:6:in `replace'
activerecord (3.2.11) lib/active_record/associations/singular_association.rb:17:in `writer'
activerecord (3.2.11) lib/active_record/associations/builder/association.rb:51:in `block in define_writers'
activerecord (3.2.11) lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
activerecord (3.2.11) lib/active_record/attribute_assignment.rb:78:in `each'
activerecord (3.2.11) lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
activerecord (3.2.11) lib/active_record/base.rb:497:in `initialize'
app/controllers/settings/companies_controller.rb:4:in `new'
app/controllers/settings/companies_controller.rb:4:in `show'
Run Code Online (Sandbox Code Playgroud)

调节器

控制器看起来像这样,但问题可能发生在使用公司模型来保存或更新另一个模型的任何位置:

class Settings::CompaniesController < SettingsController
  def show
    @company = current_user.company
    @classification = Classification.new(company: @company)
  end

  def update
  end
end
Run Code Online (Sandbox Code Playgroud)

事实/意见

一些事实和观察:

  • 问题随机发生,但通常在开发服务器运行一段时间后.
  • 生产中不会出现此问题.
  • 即使我对Company模型没有任何改变,也会出现问题.
  • 通过重新启动服务器解决了该问题.

理论

据我所知,这是由于动态加载类.

不知何故,Company类在重新加载时获取新的类标识符.我听说有关它是由于草率要求的谣言.我在公司模型中没有要求我自己,但我确实使用了 …

activerecord ruby-on-rails ruby-on-rails-3.2

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

PL/pgSQL样式指南

Internet上有很多针对不同语言的样式指针,如CSS,Javascript,Ruby,Rails等.

但是我在哪里可以找到适合PostgreSQL数据库系统 - PL/pgSQL的过程语言的现代风格指南?

我也很欣赏自动代码分析器,如红宝石的rubocop.

postgresql coding-style

9
推荐指数
2
解决办法
3170
查看次数

使用jasmine和node.js测试Web API

我们编写了一个RESTful Web API,它使用node.js响应GET和PUT请求.我们在测试API时遇到了一些困难.首先,我们使用了Zombie.js,但它没有很好地记录,所以我们无法让它来发出PUT请求:

var zombie = require("zombie");

describe("description", function() {
  it("description", function() {
    zombie.visit("http://localhost:3000/", function (err, browser, status) {
      expect(browser.text).toEqual("A")
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

之后我们尝试使用一个名为restler的REST客户端,这没关系,因为我们不需要任何高级浏览器模拟.由于请求似乎是异步的,因此测试失败,因为它在调用'on success'回调之前完成,因此失败了:

var rest = require('restler');
describe("description", function() {
  it("description", function() {
    rest.get("http://www.google.com").on('complete', function(data, response) {
      // Should fail
      expect(data).toMatch(/apa/i);
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

我们很感激有关替代测试框架或同步请求客户端的任何提示.

node.js jasmine

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

使用seed.rb时的最佳做法

我在理解如何seed.rb在rails中使用脚本时遇到了一些困难.

到目前为止,我每次部署应用程序时都使用它来填充数据库.

像这样.

seed.rb

["Video", "Tv"].each do |thing|
  Category.create(name: thing)
end
Run Code Online (Sandbox Code Playgroud)

category.rb

class Category < ActiveRecord::Base
  validates_uniqueness_of :name
end
Run Code Online (Sandbox Code Playgroud)

现在可以在每次部署或拉取时运行该脚本.开发团队中的任何人现在都可以添加自己的类别,而无需担心重复.

像这样.

人一

  • Table类别添加到seed.rb.
  • 承诺并推动掌握.

人二

  • 拉大师.
  • 运行rake db:migraterake db:seed确保本地数据库是最新的.
  • 将应用程序部署到生产服务器.rake db:seed正在服务器上运行以确保最新的数据库.

这个工作流程是否可以,如果没有,我应该在哪里放置新数据以确保每个开发人员都拥有最新的数据库?

ruby-on-rails seed ruby-on-rails-3

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

Backbone集合中消失的模型

我有一个line包含模型集合的Backbone 模型Stop.在某些时候,我想使用Underscore函数迭代线中的停靠点并获得沿线的总行程时间reduce.

但是,这不起作用.似乎某些东西在某个时刻发生了变化.它似乎只包含一个没有有意义属性的对象,尽管我知道它已经填充了四个具有有效属性的停止模型.

该模型:

App.models.Line = Backbone.Model.extend({
    initialize: function() {
        var stops = new App.models.Stops({
            model: App.models.Stop,
            id: this.get("id")
        });
        stops.fetch();
        this.set({
            stops: stops
        });
        this.set({unique: true});
        this.calculateTotalTime();
    },
    calculateTotalTime: function() {
        this.get("stops").each(function(num) {
            console.log("Model!");
        });
        console.log("Unique: ", this.get("unique"));
    }
});
Run Code Online (Sandbox Code Playgroud)

控制台打印输出是:

Model!
Unique:  true
Run Code Online (Sandbox Code Playgroud)

应该有四个"模型!",因为模型的数量是四个.

最奇怪的是在控制台中一切正常:

window.line.get("stops").each(function(num) {
            console.log("Model!");
        });
Model!
Model!
Model!
Model!
Run Code Online (Sandbox Code Playgroud)

JS使用Sprockets编译:

//= require ./init

//= require ./lib/jquery
//= require ./lib/underscore
//= require ./lib/backbone
//= require ./lib/raphael

//= require_tree ./controllers …
Run Code Online (Sandbox Code Playgroud)

backbone.js

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