草庐IT

foreign_id_of_A

全部标签

ruby-on-rails - Rails 模型 has_many 和多个 foreign_keys

相对较新的Rails并尝试使用具有名称、性别、father_id和mother_id(2个parent)的单个Person模型来建模一个非常简单的家庭“树”。下面基本上是我想做的,但显然我不能在has_many中重复:children(第一个被覆盖)。classPerson'Person'belongs_to:mother,:class_name=>'Person'has_many:children,:class_name=>'Person',:foreign_key=>'mother_id'has_many:children,:class_name=>'Person',:foreig

ruby-on-rails - Controller 规范未知关键字 : id

我有简单的Action表演defshow@field=Field.find_by(params[:id])end我想为它写规范require'spec_helper'RSpec.describeFieldsController,type::controllerdolet(:field){create(:field)}it'shouldshowfield'doget:show,id:fieldexpect(response.status).toeq(200)endend但是我有一个错误Failure/Error:get:show,id:fieldArgumentError:unknown

ruby - "wrong number of arguments (1 for 0)"在 Ruby 中是什么意思?

“参数错误:参数数量错误(1代表0)”是什么意思? 最佳答案 当您定义一个函数时,您还定义了该函数需要工作的信息(参数)。如果它被设计为在没有任何额外信息的情况下工作,并且你传递了一些信息,你就会得到那个错误。例子:不接受参数:defdogend接受参数:defcat(name)end当你调用它们时,你需要用你定义的参数来调用它们。dog#worksfinecat("Fluffy")#worksfinedog("Fido")#ReturnsArgumentError(1for0)cat#ReturnsArgumentError(0f

ruby-on-rails - rails : How to make Date strftime aware of the default locale?

我在environment.rb中将我的默认语言环境设置为de(德语)。我还看到了德语的所有错误消息,因此服务器选择了语言环境。但是当我尝试使用strftime打印日期时,如下所示:some_date.strftime('%B,%y')它以英语(January,11)打印,而不是预期的德语(Januar,11)。如何根据默认语言环境打印日期? 最佳答案 使用l(localize的别名)方法代替原始strftime,如下所示:l(date,format:'%B%d,intheyear%Y')参见here获取更多信息。您还可以定义“命名

ruby-on-rails - 在 Rails 3 中查找 session ID

如何获取Rails3中的当前sessionID?我试过以下但没有成功:session[:session_id]session['session_id']session[:id]session['id']session.idsession.session_id 最佳答案 您尝试过以下方法吗?request.session_options[:id] 关于ruby-on-rails-在Rails3中查找sessionID,我们在StackOverflow上找到一个类似的问题:

ruby-on-rails - rails : Should partials be aware of instance variables?

例如,RyanBates的nifty_scaffolding就是这样做的编辑.html.erb'form'%>new.html.erb'form'%>_form.html.erb那种隐藏的状态让我觉得不舒服,所以我通常喜欢这样做编辑.html.erb'form',:locals=>{:object=>@my_object}%>_form.html.erb那么哪个更好:a)让部分访问实例变量或b)传递部分它需要的所有变量?最近我一直选择b),但我确实遇到了一些问题:some_action.html.erb'partial',:locals=>{:son=>a_son}%>_partial

ruby-on-rails - rails : Validating min and max length of a string but allowing it to be blank

我有一个要验证的字段。我希望该字段能够留空,但如果用户正在输入数据,我希望它采用某种格式。目前我在模型中使用以下验证,但这不允许用户将其留空:validates_length_of:foo,:maximum=>5validates_length_of:foo,:minimum=>5如何编写此代码以实现我的目标? 最佳答案 你也可以使用这种格式:validates:foo,length:{minimum:5,maximum:5},allow_blank:true或者因为您的最小值和最大值相同,以下也将起作用:validates:foo

ruby 心印 : Why convert list of symbols to strings

我指的是RubyKoans中about_symbols.rb中的这个测试https://github.com/edgecase/ruby_koans/blob/master/src/about_symbols.rb#L26deftest_method_names_become_symbolssymbols_as_strings=Symbol.all_symbols.map{|x|x.to_s}assert_equaltrue,symbols_as_strings.include?("test_method_names_become_symbols")end#THINKABOUTIT:#

Ruby 自定义错误类 : inheritance of the message attribute

我似乎找不到太多关于自定义异常类的信息。我所知道的你可以声明你的自定义错误类,让它继承自StandardError,这样它就可以被rescued:classMyCustomError这允许您使用以下方式提高它:raiseMyCustomError,"Amessage"稍后,在救援时收到该消息rescueMyCustomError=>eputse.message#=>"Amessage"我不知道的事我想为我的异常提供一些自定义字段,但我想从父类继承message属性。我发现阅读onthistopic@message不是异常类的实例变量,所以我担心我的继承不起作用。任何人都可以给我更多的细

ruby-on-rails - ActiveRecord.find(array_of_ids),保留顺序

当您在Rails中执行Something.find(array_of_ids)时,结果数组的顺序不取决于array_of_ids的顺序。有什么办法可以找到并保留顺序吗?ATM我根据ID的顺序手动对记录进行排序,但这有点蹩脚。UPD:如果可以使用:order参数和某种SQL子句指定顺序,那么如何? 最佳答案 奇怪的是,没有人提出这样的建议:index=Something.find(array_of_ids).group_by(&:id)array_of_ids.map{|i|index[i].first}除了让SQL后端执行它之外,尽