Model.scoped现在不推荐使用Rails 4 .
DEPRECATION WARNING: Model.scoped is deprecated. Please use Model.all instead.
但是,有一个区别Model.scoped和Model.all,也就是说,scoped.scoped返回一个范围,而all.all运行该查询.
在Rails 3上:
> Model.scoped.scoped.is_a?(ActiveRecord::Relation)
=> true
Run Code Online (Sandbox Code Playgroud)
在Rails 4上:
> Model.all.all.is_a?(ActiveRecord::Relation)
DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. `Post.where(published: true).load`). If you want to get an array of records from a relation, you can call #to_a (e.g. `Post.where(published: true).to_a`).
=> false
Run Code Online (Sandbox Code Playgroud)
库/关注中的用例会scoped在有条件执行某些操作时返回,如下所示:
module AmongConcern
extend ActiveSupport::Concern
module …Run Code Online (Sandbox Code Playgroud) 我有3个型号:
class Student < ActiveRecord::Base
has_many :student_enrollments, dependent: :destroy
has_many :courses, through: :student_enrollments
end
class Course < ActiveRecord::Base
has_many :student_enrollments, dependent: :destroy
has_many :students, through: :student_enrollments
end
class StudentEnrollment < ActiveRecord::Base
belongs_to :student
belongs_to :course
end
Run Code Online (Sandbox Code Playgroud)
我希望查询Courses表中的课程列表,这些课程在StudentEnrollments表中与特定学生相关联.
我发现也许Left Join是要走的路,但似乎rails中的join()只接受一个表作为参数.我认为可以做我想要的SQL查询是:
SELECT *
FROM Courses c LEFT JOIN StudentEnrollment se ON c.id = se.course_id
WHERE se.id IS NULL AND se.student_id = <SOME_STUDENT_ID_VALUE> and c.active = true
Run Code Online (Sandbox Code Playgroud)
如何以Rails 4方式执行此查询?
任何输入都表示赞赏.
我正在编写一个Rails 4应用程序,它将为尚未开发的移动应用程序公开API.用户将使用移动应用程序中的电子邮件和密码进行身份验证.
虽然我已经找到了很多关于这个主题的信息.很难看出什么是过时的或非最佳的.我已经阅读了HTTP Basic Auth,它看起来似乎不太安全,以及基于HTTP令牌的Auth,但我不确定如何将它与常规电子邮件和密码认证结合起来(我正在使用Devise by方式).
我想知道目前关于如何实现这一目标的最佳实践,所以我一定会采取正确的方式.
Rails 4 Active Record Enums非常棒,但使用i18n进行翻译的正确模式是什么?
有没有可能有这样的东西?
app/models/
app/models/users/user.rb
app/models/users/education.rb
Run Code Online (Sandbox Code Playgroud)
目标是更好地组织/ app/models文件夹,但不必命名模型.
Rails 3的一个悬而未决的问题是: Rails 3.2.9和子文件夹中的模型.
使用命名空间指定table_name似乎可行(请参阅Rails 4模型子文件夹),但我想在没有命名空间的情况下执行此操作.
namespaces ruby-on-rails models subdirectory ruby-on-rails-4
我希望能够在一个表上使用两列来定义关系.所以使用任务应用程序作为示例.
尝试1:
class User < ActiveRecord::Base
has_many :tasks
end
class Task < ActiveRecord::Base
belongs_to :owner, class_name: "User", foreign_key: "owner_id"
belongs_to :assignee, class_name: "User", foreign_key: "assignee_id"
end
Run Code Online (Sandbox Code Playgroud)
那么 Task.create(owner_id:1, assignee_id: 2)
这允许我执行Task.first.owner返回用户1并Task.first.assignee返回用户2但不User.first.task返回任何内容.这是因为任务不属于用户,属于所有者和受让人.所以,
尝试2:
class User < ActiveRecord::Base
has_many :tasks, foreign_key: [:owner_id, :assignee_id]
end
class Task < ActiveRecord::Base
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
这完全失败,因为似乎没有支持两个外键.
所以我想要的是能够说出User.tasks并获得用户拥有和分配的任务.
基本上以某种方式建立一个等于查询的关系 Task.where(owner_id || assignee_id == 1)
那可能吗?
我不打算使用finder_sql,但这个问题的未被接受的答案看起来接近我想要的:Rails - …
ruby-on-rails associations model-associations ruby-on-rails-4
在我的Rails应用程序中有一个默认范围,如下所示:
default_scope order: 'external_updated_at DESC'
Run Code Online (Sandbox Code Playgroud)
我现在升级到Rails 4,当然,我得到以下弃用警告"不推荐使用散列调用#scope或#default_scope.请使用包含范围的lambda.".我已经成功转换了我的其他范围,但我不知道default_scope的语法应该是什么.这不起作用:
default_scope, -> { order: 'external_updated_at' }
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写LIKE查询.
我读到纯字符串quires不安全,但我找不到任何解释如何编写安全的LIKE Hash Query的文档.
可能吗?我应该手动防御SQL注入吗?
我知道有三个主要的符号为whereActiveRecord方法提供参数:
指定and的where方法是直截了当:
# Pure String notation
Person.where("name = 'Neil' AND age = 27")
# Array notation
Person.where(["name = ? AND age = ?", 'Neil', 27])
# Hash notation
Person.where({name: "Neil", age: 27})
Run Code Online (Sandbox Code Playgroud)
or为这个相同的where方法指定是为了哈希语法.可能吗?
# Pure String notation
Person.where("name = 'Neil' OR age = 27")
# Array notation
Person.where(["name = ? OR age = ?", 'Neil', 27])
# Hash notation DOESN'T WORK
Person.where({name: "Neil" OR age: 27})
Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails ruby-on-rails-4 rails-activerecord ruby-on-rails-5
有人能告诉我在Rails 4中执行以下行的等效方法是什么?
has_many :friends, :through => :friendships, :conditions => "status = 'accepted'", :order => :first_name
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
has_many :friends, -> { where status: 'accepted' }, :through => :friendships , :order => :first_name
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Invalid mix of scope block and deprecated finder options on ActiveRecord association: User.has_many :friends
Run Code Online (Sandbox Code Playgroud) ruby-on-rails-4 ×10
activerecord ×3
associations ×1
enums ×1
models ×1
mysql2 ×1
namespaces ×1
rails-api ×1
ruby ×1
sql ×1
subdirectory ×1