我有一个表'用户'列'电子邮件'.它曾经是唯一的(带索引),但新的要求是允许那里的nils.
有没有比以下更好的解决方案:
remove_index :users, :email
add_index :users, :email
Run Code Online (Sandbox Code Playgroud)
?
最初它添加了唯一的选项:
add_index :users, :email, :unique => true
Run Code Online (Sandbox Code Playgroud) 我遇到过以下情况:
有
ModuleA::ModuleB::ClassC.do_something
Run Code Online (Sandbox Code Playgroud)
在do_something的定义中我需要使用来自应用程序的模型
def do_something
...
data = Order.all
...
end
Run Code Online (Sandbox Code Playgroud)
但也存在一个模块
ModuleA::Order
Run Code Online (Sandbox Code Playgroud)
所以我收到了一个错误
undefined method `all' for ModuleA::Order:Module
Run Code Online (Sandbox Code Playgroud)
我找到了一个解决方案
def do_something
...
data = Kernel.const_get('Order').all
...
end
Run Code Online (Sandbox Code Playgroud)
这将返回模型.我的问题是:最好的方法是什么?有更清洁的解决方案吗?(尽管事实上,Class和Module具有相同的名称,但这不是最好的主意,但这里不能改变......)
我很困惑如何在我的rails(2.3.5)应用程序中使用json.这些是我需要的元素:
使用辅助方法(rails和我自己的方法)计算的json数据非常大,并且不会经常更改(通常是页脚,页眉和页面的其他部分),因此应该缓存它.做正确的方法是什么?
我尝试了3种方法.
一个.
- html template and javascript to render data placed in the view html.erb
- method to get json data placed in the helper
<script type="text/javascript">
var data= <%= helper_method %>;
</script>
Run Code Online (Sandbox Code Playgroud)
问题:如何缓存json数据?
B.
- html template and javascript to render data placed in the view html.erb
- method to get json data is a controller action
def footer
expires_in(4.hours)
render :json => { calculated_data }
end
- ajax request in javascript …Run Code Online (Sandbox Code Playgroud) 我有两个例子给出相同的结果.
带块:
def self.do_something(object_id)
self.with_params(object_id) do |params|
some_stuff(params)
end
end
def self.with_params(object_id, &block)
find_object_by_id
calculate_params_hash
block.call(params_hash)
end
Run Code Online (Sandbox Code Playgroud)
并与方法:
def self.do_something(object_id)
some_stuff(self.get_params(object_id))
end
def self.get_params(object_id)
find_object_by_id
calculate_params_hash
params_hash
end
Run Code Online (Sandbox Code Playgroud)
第二个解决方案似乎更直接,但我在应用程序代码中找到了第一个解决方案的一些用法.我的问题是:在哪种情况下推荐第一个?每个人的利弊是什么?
我有液体模板存储在DB中,在渲染之前,我想检查一下,如果提供了模板所需的所有参数 - 现在我发现了类似的东西:
parsed = Liquid::Template.parse(string_with_template)
required = parsed.instance_values["root"].instance_values["nodelist"].select{ |v| v.is_a?(Liquid::Variable) }.map(&:name)
Run Code Online (Sandbox Code Playgroud)
然后在渲染之前我有一个功能
def has_all_required?(liquid_params, required)
keys = liquid_params.keys
required.each{|e| return false unless keys.include?(e) }
return true
end
Run Code Online (Sandbox Code Playgroud)
有没有更简洁的方法来实现这种验证?
感谢所有的建议,Santuxus
我很难让rails 4使用nested_attributes和serialize.我有:
class Client < ActiveRecord::Base
belongs_to :event
serialize :phones
end
class Event < ActiveRecord::Base
has_one :client
end
class EventsController < ApplicationController
...
def event_params
params.permit(client_attributes: [:phones])
end
end
Run Code Online (Sandbox Code Playgroud)
当我通过活动时:
{client_attributes: { phones: 'string'}}
Run Code Online (Sandbox Code Playgroud)
它有效,但当我尝试
{client_attributes: { phones: [{phone_1_hash},{phone_2_hash}]}}
Run Code Online (Sandbox Code Playgroud)
我收到'未经许可的参数:手机'消息并且字段未保存...
我试过用
class EventsController < ApplicationController
...
def event_params
params.permit(client_attributes: [phones:[]])
end
end
Run Code Online (Sandbox Code Playgroud)
要么
class Client < ActiveRecord::Base
belongs_to :event
serialize :phones, Array
end
Run Code Online (Sandbox Code Playgroud)
但到目前为止没有任何帮助 任何建议,将不胜感激.谢谢!
serialization nested-attributes strong-parameters ruby-on-rails-4