我设置了以下型号:
class Contact < ActiveRecord::Base
belongs_to :band
belongs_to :mode
validates_presence_of :call, :mode
validates_associated :mode, :band
validates_presence_of :band, :if => :no_freq?
validates_presence_of :freq, :if => :no_band?
protected
def no_freq?
freq.nil?
end
def no_band?
band.nil?
end
end
class Band < ActiveRecord::Base
has_many :logs
end
class Mode < ActiveRecord::Base
has_many :logs
end
Run Code Online (Sandbox Code Playgroud)
当我在新视图中输入频率时,如果输入了频率,则不允许指定频段.这在我的其他视图中产生了一个问题,因为band现在为零.如何允许不指定band并在我的索引和显示视图中显示为空,然后在编辑视图中允许在稍后的时间点指定一个.
我已经能够通过执行以下操作让我的索引显示空白:
contact.band && contact.band.name
Run Code Online (Sandbox Code Playgroud)
但我不确定这是否是最好的方法,我不确定如何将类似的解决方案应用于我的其他观点.
非常感谢rails newb!
大家好我有这个代码:
@coursesFound = @user.available_courses
@courses = []
for course in @coursesFound do
@courseInGroups = course.user_groups
for group in @courseInGroups do
@group = UserGroup.find group.id
if @group.users.map { |u| u.id }.include? @user.id
@courses << course
break
end
end
end
# Wenn ein Kurs keiner Gruppe hinzugefügt wurde
if @courseInGroups.empty?
@courses << course
end
Run Code Online (Sandbox Code Playgroud)
在我的debian vm它工作正常但在我的实时系统上我得到了这个错误:
undefined method `empty?' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
我怎么能避免这个?
我有这个代码:
<div class="alert alert-success">
<a href="#" class="alert-link"><%= notice %></a>
</div>
Run Code Online (Sandbox Code Playgroud)
我需要在它周围放一个if语句,只显示整个div,如果notice不是nil.我尝试了几种方法,但无法正确使用语法.
我明白了什么.blank?,.nil?和.empty?现在一样.
我在想我为什么不全部更换.nil?,并.empty?要.blank?对犯错误少的风险.例如,if current_user.blank?
是否有性能问题的.blank?方法?是慢还是消耗更多内存?
如果是的话,有多糟糕?字符串与符号一样糟糕?
提前致谢.
这是一个关于Ruby on Rails的帮助呼吁,以阻止我的血压达到危险水平......
Ruby版本:ruby 2.1.0p0(2013-12-25修订版44422)
Rails版本:Rails 4.0.3
基本上我正在创建一个Web应用程序,可以让您获得有关设计的反馈,但是当我在测试中显示标题时,我遇到'除非'语句的问题#index page
控制器(这适用于检索正确的测试):
def index
# Gets all tests that have an end date before now
@completed_tests = Test.where(["end_date < ?", DateTime.now]).order("end_date asc")
# Gets all tests that have an end date after now
@current_tests = Test.where(["end_date > ?", DateTime.now]).order("end_date asc")
end
Run Code Online (Sandbox Code Playgroud)
查看(测试#index)(这是导致我出问题的部分)
# This should be if there are no tests for the user, display the Blank state text
<% unless @completed_tests.nil? || @current_tests.nil? %>
# If there are no completed tests …Run Code Online (Sandbox Code Playgroud) 在一种情况下,我需要加入4个范围中的4个范围,3个是用模型(A)编写的,另一个是模型(B),如何加入这些范围?
这两个模型有HABTM关系
Model A
scope 1
scope 2
scope 3
total_scope= scope1.scope2.scope3.scope4
end
Model B
scope 4
end
Run Code Online (Sandbox Code Playgroud) 一些有效的ActiveRecord对象返回false的present?:
object.nil? # => false
object.valid? # => true
object.present? # => false
object.blank? # => true
Run Code Online (Sandbox Code Playgroud)
我喜欢object.present?过not object.nil?.什么决定了present?/ 的回报值blank??
编辑:找到了答案:我已经重新定义了empty?这个类的方法,而不是blank?或present?方法; 沿着@Simone的回答,blank?正在empty?幕后使用.
我关注这个帖子。在我看来:
- if current_user
- if current_user.present?
- if current_user.exists?
- if current_user.any?
Run Code Online (Sandbox Code Playgroud)
我收到错误:
undefined method `exists?' for #<User:0x00007fd68f4067b8>
undefined method `any?' for #<User:0x00007fd68f4067b8>
Run Code Online (Sandbox Code Playgroud)
所以只有前 2 个工作。为什么?性能是否有任何差异:
- if current_user
vs
- if current_user.present?
Run Code Online (Sandbox Code Playgroud)
和
- if current_user.name
vs
- if current_user.name.present?
Run Code Online (Sandbox Code Playgroud) 我有一个 pandas 数据框,具有一行和多列。
我想获取给定行中最小值的列号/索引。
我找到的代码是: df.columns.get_loc('colname')
上面的代码要求提供列名称。我的数据框没有列名。我想获取最小值的列位置。
我有一个显示嵌套关系的表单.渲染嵌套子对象的调用如下所示:
<% if @fpimgblocks %>
??<% f.fields_for @fpimgblocks do |builder| %>
????<%= render 'fpimgblock_fields', :f => builder %>
??<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
@fpimgblocks是一个查找的结果,我已经验证了没有结果,所以我希望这不会渲染.但是,即使未初始化对象,也会呈现局部.然后,当我提交表单时,这会返回一个nil_class错误.
if语句中的语法是错误还是什么?我已经尝试过更改为"除非@ fpimgblocks.nil?但没有变化.
我在活动记录中有一个数据库对象。如果我称它为object.find(1).present?returns false,但它存在。调用!object.find(1).nil?返回true。
为什么是这样?我想!nil == present?。
在 Rails 中,我想检查数组是否为空(arr = [])。
!arr&.any?
arr&.empty?
Run Code Online (Sandbox Code Playgroud)
哪个更正确?
ruby-on-rails ×11
ruby ×9
activerecord ×1
if-statement ×1
memory ×1
pandas ×1
performance ×1
python ×1