我知道有三个主要的符号为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
我有以下两种型号:
class Store < ActiveRecord::Base
belongs_to :person
end
class Person < ActiveRecord::Base
has_one :store
end
Run Code Online (Sandbox Code Playgroud)
这是问题所在:我正在尝试创建迁移以在people表中创建外键.但是,引用Store外键的列未命名为store_id,因为它是rails约定,而是命名为foo_bar_store_id.
如果我遵循rails约定,我会像这样进行迁移:
class AddReferencesToPeople < ActiveRecord::Migration
def change
add_reference :people, :store, index: true
end
end
Run Code Online (Sandbox Code Playgroud)
但是这不起作用,因为列名不是store_id,而是foo_bar_store_id.那么我如何指定外键名称只是不同,但仍保持索引:true以保持快速性能?
原始问题
两个资源:users和animals. 创建用户时,客户端选中复选框以说明他们有多少动物.
提交用户表单时,它不仅应创建新user记录,还应在animal_users富连接表中创建一组记录,以表示客户端选择的每个复选框.
我认为的问题是我没有为表单中的复选框部分正确指定内容.我查看了checkbox_tag API,表单上的Rails指南以及许多网站和stackOverflow帖子.
在此先感谢,代码如下:
原始代码(答案代码进一步向下):
楷模:
#models/user.rb
class User < ActiveRecord::Base
has_many :animals, through: :animal_users
has_many :animal_users
accepts_nested_attributes_for :animal_users, allow_destroy: true
end
#models/animal.rb
class Animal < ActiveRecord::Base
has_many :users, through: :animal_users
has_many :animal_users
end
#models/animal_user.rb
class AnimalUser < ActiveRecord::Base
belongs_to :animal
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
该user表格:
#views/users/_form.html.erb
<%= form_for(@user) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field …Run Code Online (Sandbox Code Playgroud) 当我运行我的功能规范时,我收到此消息:
弃用警告:不推荐使用ActiveRecord :: Base.raise_in_transactional_callbacks =,无效,将在不更换的情况下删除.
我正在使用Rails 5.0.0.rc1,我不确定是什么抛出了这个弃用警告.
我在我的application.rb文件中有这个.我删除了它,弃用警告消失了:
config.active_record.raise_in_transactional_callbacks = true
Run Code Online (Sandbox Code Playgroud)
我想知道这个弃用警告实际意味着什么,并知道什么触发了这个弃用警告.
我需要获取正在使用的磁盘上的文件的路径ActiveStorage.该文件存储在本地.
当我使用paperclip时,我path在附件上使用了返回完整路径的方法.
例:
user.avatar.path
Run Code Online (Sandbox Code Playgroud)
在查看Active Storage Docs时,它似乎rails_blob_path可以解决问题.在查看它返回的内容之后,它没有提供文档的路径.因此,它返回此错误:
没有这样的文件或目录@ rb_sysopen -
背景
我需要文档的路径,因为我使用的是combine_pdf gem,以便将多个pdf组合成一个pdf.
对于回形针实现,我迭代了所选pdf附件的full_paths,并将load它们组合成合并的pdf:
attachment_paths.each {|att_path| report << CombinePDF.load(att_path)}
Run Code Online (Sandbox Code Playgroud) 我用以下内容编写了一个迁移:
class CreateTableSomeTable < ActiveRecord::Migration[5.1]
def change
create_table :some_tables do |t|
t.references :user, foreign_key: true
t.references :author, references: :user, foreign_key: true
t.text :summary
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是一个创建数据库表的基本迁移.但是:当我运行rails db:migrate一个非常奇怪的错误消息时中止迁移:
Mysql2 ::错误:表'my_database.some_tables'不存在:显示来自'some_tables'的完整字段
好像错误是说它无法创建表,因为表确实存在,这没有意义.
我看过并试过的事情:
bundle确保所有的宝石都安装好了schema.rb文件,用另一个副本中的数据重新创建数据库,然后我跑去rake db:schema:dump重新创建schema.rb文件.我试图再次运行迁移,但仍然遇到了同样的错误.我使用rails 5.1.1以及mysql2 0.4.6
有关如何让迁移运行的任何提示?
我想在ActiveRecord::Relation对象中找到特定记录,以便我可以获取该记录的属性.
以下工作,但问题是它使用该find_by语句再次访问数据库.它不应该.rails应该有一种方法可以在对象中找到该ActiveRecord::Relation对象,而不必再次查询数据库.
#returns an ActiveRecord::Relation object
@blogs = Blog.all
# Search for the blog within that ActiveRecord::Relation object, NOT the database
@blogs.find_by(id: 1).title #do this statement but don't hit the database again
Run Code Online (Sandbox Code Playgroud) 提前类型功能适用于应有的位置.但问题是,类型提前功能是在数据的每个请求上发出JSON请求时,它实际上只发生在一个特定请求上.
我有以下控制器:
#controllers/agencies_controller.rb
class AgenciesController < ApplicationController
def get_unique_agency_names
@unique_agency_names = Agency.uniq.pluck(:name)
respond_to do |format|
format.json { render json: @unique_agency_names }
end
end
...
end
Run Code Online (Sandbox Code Playgroud)
我的javascript文件中有以下内容:
#app/assets/javascripts.agencies/index.js
$(document).ready(function(){
/* For typeahead functionality on name input of search form for agencies */
var agency_names = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: '../agencies/get_unique_agency_names.json'
});
$('#prefetch .typeahead.name_input').typeahead(null, {
name: 'agency_names',
source: agency_names
});
});
Run Code Online (Sandbox Code Playgroud)
只是为了完成,这里是我希望这个功能发生的地方:在这种形式:
# just showing the relevant part of the form
<div class="form-group" id="prefetch">
<%= f.label :name_cont, "Agency Name" …Run Code Online (Sandbox Code Playgroud) 我发现很少有关于如何在rails中编写多态关联的范围,更不用说如何在多态关联上编写查询.
在Rails文档中,我查看了" 多态关联"部分," 连接表"部分和" 范围"部分.我也做了相当广泛的谷歌搜索.
以此设置为例:
class Pet < ActiveRecord::Base
belongs_to :animal, polymorphic: true
end
class Dog < ActiveRecord::Base
has_many :pets, as: :animal
end
class Cat < ActiveRecord::Base
has_many :pets, as: :animal
end
class Bird < ActiveRecord::Base
has_many :pets, as: :animal
end
Run Code Online (Sandbox Code Playgroud)
所以Pet可以是animal_type"狗","猫"或"鸟".
要显示所有表结构:这是我的schema.rb:
create_table "birds", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "cats", force: :cascade do |t|
t.integer "killed_mice"
t.datetime "created_at", null: false …Run Code Online (Sandbox Code Playgroud) 我把binding.pry控制器的update动作放在了最顶层.一旦到达那个断点,我params[:foo_bar]就会检查params哈希值.这是我得到的:
<ActionController::Parameters {"utf8"=>"?", "_method"=>"patch", "authenticity_token"=>"123==", "foobar"=><ActionController::Parameters {"barbazz_attributes"=>{"start_date"=>"08/27/2016", "end_date"=>"08/29/2016", "id"=>"89"}, "bazz_id"=>"3", "abc_id"=>"330", "bazzbazz_attributes"=>{"0"=>{"_destroy"=>"1", "city_id"=>"1669", "id"=>"26"}, "1"=>{"city_id"=>"1681", "id"=>"27"}, "2"=>{"city_id"=>"1672"}}} permitted: false>, "cat_id"=>["1", "1", "1"], "commit"=>"Update FooBar", "controller"=>"foo_bars", "action"=>"update", "id"=>"52"} permitted: false>
Run Code Online (Sandbox Code Playgroud)
我假设permitted: false是因为我没有将某些属性列入白名单.我查看了属性,在我看来,我确实将所有内容列入白名单.
我正在使用Rails 5,如果这恰好有任何区别.
问题:找出强参数返回原因的简便方法是什么params: false.