我确实发现了一些关于Rails关联的问题,这些关联有点像我的问题,但对于我的生活,我似乎无法理解如何使用belongs_to多个模型.
这是我打算拥有的表结构:
User
id
Post
id
user_id #foreign key; a post belongs to a User aka "Who created this post"
Comment
id
user_id #foreign key; a comment belongs to a User aka "Who made this comment"
post_id #foreign key; a comment belongs to a Post aka "What post this comment is for"
Run Code Online (Sandbox Code Playgroud)
而协会:
User
has_many :posts
has_many :comments
Post
belongs_to :user
has_many :comments
Comment
belongs_to :user
belongs_to :post
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?
在Rails中,find_each和where用于从ActiveRecord支持的数据库中检索数据.
您可以将查询条件传递给where,例如:
c = Category.where(:name => 'Ruby', :position => 1)
Run Code Online (Sandbox Code Playgroud)
您可以将批量大小传递给find_each,例如:
Hedgehog.find_each(batch_size: 50).map{ |p| p.to_json }
Run Code Online (Sandbox Code Playgroud)
但是以下两个代码有什么区别?
# code 1
Person.where("age > 21").find_each(batch_size: 50) do |person|
# processing
end
# code 2
Person.where("age > 21").each do |person|
# processing
end
Run Code Online (Sandbox Code Playgroud)
代码1批次每次检索50个元组,代码2一次检索所有元组吗?欢迎更多细节解释.
我的意见是:
where和find_each可用于批量检索,但是用户可使用时限定批量大小find_each.find_each 不支持传递查询条件.如果我的理解是错误的,请纠正我.
ruby activerecord ruby-on-rails ruby-on-rails-3 ruby-on-rails-4
可以这样做吗?在单个应用程序中,使用SQLite管理许多项目.我想要的是为我的应用程序管理的每个项目都有一个不同的数据库..所以同一结构化数据库的多个副本,但其中包含不同的数据.我将根据URI上的params选择使用哪个副本.
这是为1.安全性做的..我是这种编程的新手,我不希望它发生在某个原因上,在工作项目时,另一个被破坏了.2.易于备份和存档旧项目
在Ruby 1.9.2on中Rails 3.0.3,我试图测试两个Friend(类继承自ActiveRecord::Base)对象之间的对象相等性.
对象相等,但测试失败:
Failure/Error: Friend.new(name: 'Bob').should eql(Friend.new(name: 'Bob'))
expected #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil>
got #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil>
(compared using eql?)
Run Code Online (Sandbox Code Playgroud)
只是为了笑容,我还测试了对象身份,它失败了,正如我所料:
Failure/Error: Friend.new(name: 'Bob').should equal(Friend.new(name: 'Bob'))
expected #<Friend:2190028040> => #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil>
got #<Friend:2190195380> => #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil>
Compared using …Run Code Online (Sandbox Code Playgroud) 我想知道在创建ActiveRecord对象时调用的回调和验证的顺序.
假设我有一些自定义验证和回调,如下所示:
validates :reference_code, :if => :reference_code, :on => :create
before_create :assign_reference
Run Code Online (Sandbox Code Playgroud)
哪一个先运行?回调需要先发生,否则验证可能会失败.
我认真对待命名,所以我认为我的ActiveRecord模型的名字很难.但是,我经常提出一个名称,它与数据库或Ruby或Rails中的保留名称有一些冲突.模型或字段名称如set或group.有时问题也不会立即显现出来.我们的模型名称和字段名称列表中是否有名称的名称?
activerecord ruby-on-rails ruby-on-rails-3 active-record-query ruby-on-rails-3.2
我正在使用Rails 3.0.3并且已经在数据库中拥有我的"类别"表的数据,但是想要从中创建种子文件.是否有任何rake任务将从此表中为我生成seeds.rb格式?
我是ActiveRecord新查询界面的新手,所以我还在搞清楚.
我希望有人可以解释scope在ActiveRecord模型中使用a和使用类方法之间的区别(即self.some_method)
从我可以收集的内容来看,范围总是希望返回一个关系,而一个类方法不一定必须.这是真的?
例如,我认为做以下事情是有意义的:
class Person
scope :grouped_counts, group(:name).count
end
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我收到此错误:
ArgumentError: Unknown key(s): communicating, failed, matched, unmatched
from /Users/bradrobertson/.rvm/gems/ruby-1.9.2-p180@influitive/gems/activesupport-3.0.5/lib/active_support/core_ext/hash/keys.rb:43:in `assert_valid_keys'
from /Users/bradrobertson/.rvm/gems/ruby-1.9.2-p180@influitive/gems/activerecord-3.0.5/lib/active_record/relation/spawn_methods.rb:110:in `apply_finder_options'
from /Users/bradrobertson/.rvm/gems/ruby-1.9.2-p180@influitive/gems/activerecord-3.0.5/lib/active_record/named_scope.rb:110:in `block in scope'
from (irb):48
from /Users/bradrobertson/.rvm/gems/ruby-1.9.2-p180@influitive/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start'
from /Users/bradrobertson/.rvm/gems/ruby-1.9.2-p180@influitive/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start'
from /Users/bradrobertson/.rvm/gems/ruby-1.9.2-p180@influitive/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
r
Run Code Online (Sandbox Code Playgroud)
但它确实可以作为一种类方法
def self.grouped_counts
group(:name).count
end
Run Code Online (Sandbox Code Playgroud)
我很想知道人们关于何时使用范围以及何时使用类方法的想法.我是否正确假设范围必须始终返回关系,但是类方法可以返回它想要的任何内容?
我是否可以在ActiveRecord,Ruby on Rails中创建和使用包含no :id列的数据库表.
我不仅要忽略id列,而且希望它绝对不存在.
Table Example
:key_column :value_column
0cc175b9c0f1b6a831c399e269772661 0cc175b9c0f1b6a831c399e269772661
4a8a08f09d37b73795649038408b5f33 0d61f8370cad1d412f80b84d143e1257
92eb5ffee6ae2fec3ad71c777531578f 9d5ed678fe57bcca610140957afab571
Run Code Online (Sandbox Code Playgroud)
任何更多信息(如:id_column)都会破坏整个功能.
我如何在rails中实现这样的东西?
我正在为库存管理编写Rails前端.我希望用户能够注册产品,所以我有:
class User < ActiveRecord::Base
has_many :products
# <snip>
end
Run Code Online (Sandbox Code Playgroud)
和
class Product < ActiveRecord::Base
belongs_to :user
# <snip>
end
Run Code Online (Sandbox Code Playgroud)
问题是产品是在用户注册之前创建的.也就是说,这是完全可以接受的呼吁Product.create,只是把它设置user_id到nil.但是,正如您可以想象的那样,Rails不支持开箱即用:
> Product.create!
(0.3ms) SELECT COUNT(*) FROM "products" WHERE "products"."type" IN ('Product')
(0.1ms) begin transaction
(0.1ms) rollback transaction
ActiveRecord::RecordInvalid: Validation failed: User can't be blank
from ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-3.2.13/lib/active_record/validations.rb:56:in `save!'
Run Code Online (Sandbox Code Playgroud)
我已经考虑过一堆kludgey解决方法,其中最吸引人的是有一个NullUser子类User并使用它来创建产品.但这似乎仍然是一个黑客.这是什么Rails方式?
谢谢.
相关迁移:
class AddUseridToProducts < ActiveRecord::Migration
def change
add_column :products, :user_id, :integer
end
end
Run Code Online (Sandbox Code Playgroud)
然后:
class Changeuseridtobeoptionalforproducts < ActiveRecord::Migration
def change …Run Code Online (Sandbox Code Playgroud) activerecord ×10
ruby ×4
belongs-to ×1
class-method ×1
database ×1
equality ×1
identity ×1
mysql ×1
rake ×1
scopes ×1
security ×1
validation ×1