引用Rails 4.2 add_foreign_key支持:
# add a foreign key to `articles.author_id` referencing `authors.id`
add_foreign_key :articles, :authors
Run Code Online (Sandbox Code Playgroud)
如何创建一个可以为空的外键约束,允许这种情况,哪里articles.author_id可以有时为null?
ruby ruby-on-rails ruby-on-rails-4 rails-activerecord ruby-on-rails-4.2
我找到了一个答案,其中有一些可用的having例子可以找到有n孩子的父母,但同样不能用于找到没有孩子的父母(大概是因为连接不包括他们).
scope :with_children, joins(:children).group("child_join_table.parent_id").having("count(child_join_table.parent_id) > 0")
Run Code Online (Sandbox Code Playgroud)
谁能指出我正确的方向?
rails控制台输出如下所示:
User.all
=> [#<User id: 1, name: "Michael Hartl", email: "mhartl@example.com",
created_at: "2011-12-05 00:57:46", updated_at: "2011-12-05 00:57:46">,
#<User id: 2, name: "A Nother", email: "another@example.org", created_at:
"2011-12-05 01:05:24", updated_at: "2011-12-05 01:05:24">]
Run Code Online (Sandbox Code Playgroud)
我想知道是否有命令可以让它更容易阅读?例如,MongoDB控制台中有一个.pretty命令,它将输出格式化得更加友好.但不确定Rails中是否存在类似的东西.
如果有办法我可以在Rails中验证单个属性?
就像是:
ac_object.valid?(attribute_name)
Run Code Online (Sandbox Code Playgroud)
我将它用于特定模型字段的AJAX验证.将这些验证移动到Javascript会使代码变得非常难看.
我有模特简介.个人资料has_one用户.用户模型有现场电子邮件.我打电话的时候
Profile.some_scope.includes(:user)
Run Code Online (Sandbox Code Playgroud)
它叫
SELECT users.* FROM users WHERE users.id IN (some ids)
Run Code Online (Sandbox Code Playgroud)
但我的用户模型有很多字段,我没有在渲染中使用.是否可以仅加载用户的电子邮件?所以,SQL应该是这样的
SELECT users.email FROM users WHERE users.id IN (some ids)
Run Code Online (Sandbox Code Playgroud) 我想在列上指定唯一索引,但我还需要允许NULL值(多个记录可以包含NULL值).使用PostgreSQL进行测试时,我发现我可以拥有一条带有NULL值的记录,但下一个会导致问题:
irb(main):001:0> u=User.find(5)
User Load (111.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 5]]
=> #<User id: 5, email: "a@b.com", created_at: "2013-08-28 09:55:28", updated_at: "2013-08-28 09:55:28">
irb(main):002:0> u.email=nil
=> nil
irb(main):003:0> u.save
(1.1ms) BEGIN
User Exists (4.8ms) SELECT 1 AS one FROM "users" WHERE ("users"."email" IS NULL AND "users"."id" != 5) LIMIT 1
(1.5ms) ROLLBACK
=> false
Run Code Online (Sandbox Code Playgroud)
因此,即使数据库允许,Rails也会首先检查是否User存在具有不同id且email列设置为NULL.有没有一种方法,不仅数据库可以允许它,但Rails也不会像上面那样先检查?
这个想法是用户不必输入电子邮件,但如果他们这样做,我需要能够通过他们的电子邮件找到用户.我知道我可以创建另一个模型来将用户与电子邮件相关联,但我更愿意以上述方式进行操作.
更新:这是我为添加email列而创建的迁移代码:
class …Run Code Online (Sandbox Code Playgroud) postgresql activerecord ruby-on-rails ruby-on-rails-4 rails-activerecord
我一直在调试这个Rails的奇怪问题,给我"桌面的未知主键......",即使桌子的ID在那里.
我已经将数据库从一个heroku应用程序复制到另一个,在原始数据库上没有问题,新的一个给了我一个db错误.
这是错误:
ProductsController# (ActionView::Template::Error) "Unknown primary key for table collections in model Collection."
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/reflection.rb:366:in `primary_key'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/reflection.rb:480:in `association_primary_key'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:58:in `block in add_constraints'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:39:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:39:in `each_with_index'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:39:in `add_constraints'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:31:in `scope'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:98:in `association_scope'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:87:in `scoped'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:573:in `first_or_last'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:105:in `last'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/collection_proxy.rb:46:in `last'
/app/app/helpers/likes_helper.rb:62:in `significant_liker'
Run Code Online (Sandbox Code Playgroud)
引起它的那条线:
product.collections.last.try :user
Run Code Online (Sandbox Code Playgroud)
和表:
d8apjspa441pad=> \d collections
Table "public.collections"
Column | Type | Modifiers
----------------+------------------------+----------------------------------------------------------
id | integer | not null default nextval('collections_id_seq'::regclass)
name | character varying(255) |
user_id | integer |
permalink | character varying(255) |
category_id …Run Code Online (Sandbox Code Playgroud) postgresql ruby-on-rails heroku rails-activerecord heroku-postgres
如果字段不为空,我如何限制Rails验证仅检查创建OR?我正在为我正在处理的应用程序创建一个用户设置页面,问题是,当使用表单提供的参数进行更新时,只有在存在密码和密码确认时才会保存设置.我希望这些密码字段在创建时验证,无论如何,但仅在提供时更新.
我有一个模型功能,我想确保使用一个事务.例如:
class Model
def method
Model.transaction do
# do stuff
end
end
end
Run Code Online (Sandbox Code Playgroud)
我当前的方法是在块内部存根方法调用以引发ActiveRecord::Rollback异常,然后检查数据库是否实际已更改.但这意味着如果由于某种原因,块内的实现发生了变化,那么测试就会中断.
你会怎么测试这个?