这个问题与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. …
我在我的应用程序中的不同位置出现此错误:
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类在重新加载时获取新的类标识符.我听说有关它是由于草率要求的谣言.我在公司模型中没有要求我自己,但我确实使用了 …
我们编写了一个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)
我们很感激有关替代测试框架或同步请求客户端的任何提示.
我在理解如何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:migrate并rake db:seed确保本地数据库是最新的.rake db:seed正在服务器上运行以确保最新的数据库.这个工作流程是否可以,如果没有,我应该在哪里放置新数据以确保每个开发人员都拥有最新的数据库?
我有一个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)