我不知道出了什么问题,但我无法使用belongs_to:class_name选项.有人可以启发我吗?非常感谢!
这是我的代码中的一个剪辑.
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.text :name
end
end
def self.down
drop_table :users
end
end
#####################################################
class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.text :title
t.integer :author_id, :null => false
end
end
def self.down
drop_table :books
end
end
#####################################################
class User < ActiveRecord::Base
has_many: books
end
#####################################################
class Book < ActiveRecord::Base
belongs_to :author, :class_name => 'User', :validate => true
end
#####################################################
class BooksController < ApplicationController
def create
user = User.new({:name …
Run Code Online (Sandbox Code Playgroud) 我目前正在克隆这样的单级关联:
class Survey < ActiveRecord::Base
def duplicate
new_template = self.clone
new_template.questions << self.questions.collect { |question| question.clone }
new_template.save
end
end
Run Code Online (Sandbox Code Playgroud)
因此克隆那个Survey
克隆Questions
与该调查相关联.精细.这非常有效.
但我遇到的问题是每个问题has_many
Answers
.所以Survey has_many Questions which has_many Answers
.
我无法弄清楚如何正确克隆答案.我试过这个:
def duplicate
new_template = self.clone
self.questions.each do |question|
new_question = question.clone
new_question.save
question.answers.each do |answer|
new_answer = answer.clone
new_answer.save
new_question.answers << answer
end
new_template.questions << question
end
new_template.save
end
Run Code Online (Sandbox Code Playgroud)
但实际上替换原始答案然后创建新答案会产生一些奇怪的东西,因此ID会正确地停止匹配.
activerecord clone ruby-on-rails associations ruby-on-rails-3
我试图首先在EF代码中建立多对多关系,但默认约定是错误的.以下类描述了这种关系:
class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
class Account
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
一个帐户可以有许多产品.
但是,EF约定将创建DB表,如下所示:
Products Table
--------------
Id
Name
Account_Id <- What is this?
Accounts Table
--------------
Id
Name
Run Code Online (Sandbox Code Playgroud)
这看起来不像是一个多对多的表结构?如何配置流畅的API以反映关系并创建中间表:
AccountProducts Table
---------------------
Account_Id
Product_Id
Run Code Online (Sandbox Code Playgroud) 这可能很简单,但我无法在任何地方找到一个例子.
我有两个工厂:
FactoryGirl.define do
factory :profile do
user
title "director"
bio "I am very good at things"
linked_in "http://my.linkedin.profile.com"
website "www.mysite.com"
city "London"
end
end
FactoryGirl.define do
factory :user do |u|
u.first_name {Faker::Name.first_name}
u.last_name {Faker::Name.last_name}
company 'National Stock Exchange'
u.email {Faker::Internet.email}
end
end
Run Code Online (Sandbox Code Playgroud)
我想要做的是在创建配置文件时覆盖一些用户属性:
p = FactoryGirl.create(:profile, user: {email: "test@test.com"})
Run Code Online (Sandbox Code Playgroud)
或类似的东西,但我不能正确的语法.错误:
ActiveRecord::AssociationTypeMismatch: User(#70239688060520) expected, got Hash(#70239631338900)
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过首先创建用户然后将其与配置文件相关联来实现此目的,但我认为必须有更好的方法.
或者这将工作:
p = FactoryGirl.create(:profile, user: FactoryGirl.create(:user, email: "test@test.com"))
Run Code Online (Sandbox Code Playgroud)
但这似乎过于复杂.是否有更简单的方法来覆盖关联的属性?这是什么语法?
我很难弄清楚如何将我的一个模型与另一个模型联系起来.
就像现在一样,我有:
class ModelA < ActiveRecord::Base
has_many :model_b
end
class ModelB < ActiveRecord::Base
belongs_to :model_a
end
Run Code Online (Sandbox Code Playgroud)
但是...... ModelB不仅需要属于ModelA的一个实例,而且可能属于三个.我知道有一个has_many:through,但我不确定它在这种情况下会如何起作用.ModelA的每个实例总是会有三个ModelB实例.但如前所述,ModelB不仅可以属于ModelA的一个实例.
ruby-on-rails associations model-associations ruby-on-rails-3
我期待编写一个范围,返回所有没有特定关联的记录.
foo.rb
class Foo < ActiveRecord::Base
has_many :bars
end
Run Code Online (Sandbox Code Playgroud)
bar.rb
class Bar < ActiveRecord::Base
belongs_to :foo
end
Run Code Online (Sandbox Code Playgroud)
我希望有一个范围,可以找到所有的Foo's
那不要有任何bars
.很容易找到使用关联的那些joins
,但我还没有找到相反的方法.
我在rails中使用paper_trail来跟踪我的模型版本.但在GitHub库的文件表明,创业板不支持has_many
,belongs_to
协会.
假设我有一个应用程序记录了一些comapnies的ceos名称:
class Company < ActiveRecord::Base
has_many :ceos
has_paper_trail
end
class Ceo < ActiveRecord::Base
belongs_to :companies
has_paper_trail
end
Run Code Online (Sandbox Code Playgroud)
以上示例代表ABC Inc.的信息.
company.name => "ABC"
company.ceo.past => "John Henry"
company.ceo.present => "Amy Warren"
Run Code Online (Sandbox Code Playgroud)
如何实施以下操作,以便将公司和公司的ceos名称重置为上一版本?
inverse_of
在mongoid协会中意味着什么?如果没有它,我可以通过使用它而不仅仅是关联来获得什么?
我已经暂时安装了2012,但我还必须安装VS 2010.正如预期的那样,我所有的文件关联现在都指向VS2010并且图标不是那么直观.如果不通过资源管理器菜单手动执行每个文件,如何快速恢复/更改关联到VS2012?
我有一个Customer_ratings模型,允许用户互相留下反馈.Web应用程序正常运行,并收集,存储和显示反馈.
我想通过rails控制台进入并删除一些反馈,但是当我输入时Customer_rating.all
,我收到以下错误:
LoadError: Unable to autoload constant Customer_rating, expected /Users/myapps/app/models/customer_rating.rb to define it
Run Code Online (Sandbox Code Playgroud)
同样,如果我输入Customer_rating [0],我会得到:
RuntimeError: Circular dependency detected while autoloading constant Customer_rating
Run Code Online (Sandbox Code Playgroud)
通过我的控制台访问其他表时我没有这个问题.
可能导致此问题的原因,以及为什么此错误不会禁止Customer_ratings通过Web应用程序正常工作?
associations ×10
activerecord ×2
autoload ×1
c# ×1
clone ×1
factory-bot ×1
file ×1
has-many ×1
mongoid ×1
relationship ×1
rspec ×1
ruby ×1
scopes ×1
sql ×1