知道默认情况下 Rails 按 ID 对数据进行排序,那么如何按给定 where 子句的 id 进行排序呢?
ids = Bookmark.where(user_id: 7).order(created_at: :desc).pluck(:company_id)
Run Code Online (Sandbox Code Playgroud)
结果:
[146, 140, 128, 4, 2]
现在,当我尝试以相同的顺序获取这些公司时ids
Company.where(id: ids).pluck(:id)
Run Code Online (Sandbox Code Playgroud)
结果:
[2、4、128、140、146]
预期结果:
[146, 140, 128, 4, 2]
我假装的结果在两种情况下都是相同的(相同的顺序)。返回公司的顺序应与创建该公司的书签的顺序相同。
我正在 Rails 控制台中寻找一种方法来查找以特定字符串结尾的任何值,并从值中删除该字符串。
大致如下:
Model.all.each{|x| x.duration.slice!(" weeks")}.where("duration IS NOT NULL")
Run Code Online (Sandbox Code Playgroud)
因此,诸如“47 周”之类的值只是变成“47”,但没有 Slice 方法ActiveRecord/ActiveModel,而且我不知道等效的方法。
有任何想法吗??
提前致谢...
我的应用程序使用带有 的普通 Ruby 类ActiveModel::Validations,而没有实现ActiveRecord:
class Car
include ::ActiveModel::Validations
attr_accessor :engine
end
class Engine
include ::ActiveModel::Validations
attr_accessor :cylinders
validates_presence_of :cylinders
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我想Car检查 的嵌套属性。ActiveModel::Validationsengine
car = Car.new
car.engine = Engine.new
car.engine.valid? # => false
car.valid? # => true
# It should return 'false',
# because 'engine.cylinders' is 'nil'
Run Code Online (Sandbox Code Playgroud)
获得这种行为的最简单方法是什么?
当从控制器使用序列化器时,我可以像这样向它传递额外的选项
render json: user, some_option: 'foobar
Run Code Online (Sandbox Code Playgroud)
然后我可以some_option在序列化器中引用为
serialization_options[:some_option]
Run Code Online (Sandbox Code Playgroud)
但是,如果我直接调用序列化器
MySerializer.new(user, some_option: 'foobar')
Run Code Online (Sandbox Code Playgroud)
我无法获得额外的选项,因为serialization_options它是一个空对象。
我使用postgre数据库创建了一个 Rails API ,其中模型(表)名称为coaching_event
注意:不使用脚手架并使用rails-5
我的辅导事件架构(迁移)如下:
class CreateCounselingEvent < ActiveRecord::Migration[5.0]
def change
create_table :counseling_event do |t|
t.text :name
t.datetime :start_time
t.datetime :end_time
t.text :location
t.integer :user_id
t.integer :role_id
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
我在上表中添加了一列,其类型为枚举。列名称是 event_type
现在,在我的控制器(即 CounselingEventController)中,在我的操作索引中,我正在从 coaching_event 表中检索数据。代码如下。
def index
@counseling_event = CounselingEvent.where(start_time: "2016-10-30".."2016-12-11")
render body: @counseling_event
end
Run Code Online (Sandbox Code Playgroud)
当我通过以下路线从前端调用此 api 控制器操作时
get 'counseling_event/index'
Run Code Online (Sandbox Code Playgroud)
我在控制台中收到此错误:
CounselingEvent Load (1.5ms) SELECT "counseling_event".* FROM "counseling_event"
NoMethodError (undefined method `empty?' for #<CounselingEvent:0x47d1d58>):
activemodel (5.0.0.1) lib/active_model/attribute_methods.rb:433:in `method_missing'
rack (2.0.1) …Run Code Online (Sandbox Code Playgroud) 我试图将数字存储3980040429为对象的 ID,但出现以下错误:
ActiveModel::RangeError (3980040429 is out of range for ActiveModel::Type::Integer with limit 4 bytes)\nRun Code Online (Sandbox Code Playgroud)\n\n4个字节不等于吗4 294 967 296?
4 bytes \xc3\x97 8 bits = 32 bits \n2\xc2\xb3\xc2\xb2 = 4 294 967 296\nRun Code Online (Sandbox Code Playgroud)\n\n因为我显然错了 - 我可以为 ID 保存的最大整数是多少?
\n\n我使用的是 Rails 5.2 和 Postgres 9.6。
\n我有三个型号.销售,物品和图像.我想验证,在创建促销时,每次促销至少有三张照片和一件或多件商品.实现这一目标的最佳方法是什么?
销售模式:
class Sale < ActiveRecord::Base
has_many :items, :dependent => :destroy
has_many :images, :through => :items
accepts_nested_attributes_for :items, :reject_if => lambda { |a| a[:title].blank? }, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)
物品型号:
class Item < ActiveRecord::Base
belongs_to :sale, :dependent => :destroy
has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images
end
Run Code Online (Sandbox Code Playgroud)
图像型号:
class Image < ActiveRecord::Base
belongs_to :item, :dependent => :destroy
end
Run Code Online (Sandbox Code Playgroud) 我有以下型号:
class Contact
attr_accessor :name, :emails, :message
def initialize(attrs = {})
attrs.each do |k, v|
self.send "#{k}=", v
end
end
def persisted?
false
end
end
Run Code Online (Sandbox Code Playgroud)
我在我的视图中呼吁联系表格如下:
<div class="email_form">
<%= render 'form' %>
</div>
Run Code Online (Sandbox Code Playgroud)
这是控制器:
class ShareController < ApplicationController
layout "marketing_2013"
respond_to :html, :js
def index
@contact = Contact.new
end
end
Run Code Online (Sandbox Code Playgroud)
这是表格:
<%= form_for(@contact) do |f| %>
<%= f.label :name, "Your Name" %>
<%= f.text_field :name %>
<%= f.label :text, "Send to (separate emails with a comma)" %>
<%= f.text_field :emails …Run Code Online (Sandbox Code Playgroud) 我们正在尝试添加多个可接受的对象,用户可以在其中收藏许多不同的对象,但不确定如何使其工作.
这是最喜欢的型号:
class Favorite < ActiveRecord::Base
# belongs_to :imageable, polymorphic: true
belongs_to :user
belongs_to :category
belongs_to :business
belongs_to :ad_channel
belongs_to :location
belongs_to :offer
end
Run Code Online (Sandbox Code Playgroud)
用户模型:
class User < ActiveRecord::Base
has_many :favorites, as: :favoritable
end
Run Code Online (Sandbox Code Playgroud)
一个可以被收藏的例子模型:
class Category < ActiveRecord::Base
has_many :sub_categories
has_many :ad_channels
has_many :offers
belongs_to :favoritable, polymorphic: true
end
Run Code Online (Sandbox Code Playgroud)
我不确定这是否设置正确,这是我们首先需要一些反馈.
其次,我们如何为用户"喜欢"某些东西?
这是我们迄今为止尝试过的失败:
@user.favorites << Category.find(1)
Run Code Online (Sandbox Code Playgroud)
编辑:这还需要一个收藏数据库表来记录事物吗?这对我们来说是一个非常新的概念.
ruby-on-rails polymorphic-associations activemodel rails-activerecord
我有一个模型Action,有一个
belongs_to :actor, polymorphic: true
Run Code Online (Sandbox Code Playgroud)
这演员可以是:一Customer,Admin,Seller或Guest。
我想将 的实例过滤Action为仅由特定Seller或Guest. 我怎样才能做到这一点?
在正常关联中,我会加入两个表,但是这样,我不知道如何正确执行。