草庐IT

model_instance

全部标签

ruby-on-rails - "Can' t mass-assign protected attributes"with nested protected models

我正在尝试让这个嵌套模型正常工作。我已经尝试了所有形式的复数/单数,完全删除了attr_accessible,谁知道还有什么。餐厅.rb:#==RESTAURANTMODEL##Tablename:restaurants##id:integernotnull,primarykey#name:string(255)#created_at:datetimenotnull#updated_at:datetimenotnull#classRestaurant:jobshas_many:positionsaccepts_nested_attributes_for:jobs,:allow_dest

ruby - 在没有 'instance' 引用的情况下调用 Ruby Singleton 的方法

我想在不引用其实例的情况下调用单例对象的方法SingletonKlass.my_method代替SingletonKlass.instance.my_method我想出了这个解决方案(在类里面使用method_missing):require'singleton'classSingletonKlassincludeSingletondefself.method_missing(method,*args,&block)self.instance.send(method,*args)enddefmy_methodputs"hithere!!"endend这有什么缺点吗?有没有更好的解决方案

ruby - 在 RSpec instance_double 上 stub 一个 setter

在RSpec单元测试中,我有一个像这样定义的模拟:let(:point){instance_double("Point",:to_coords=>[3,2])}在Point类中,我还有一个setter,用于被测类(称为Robot)。我想stub那个setter来测试Robot#move。这是我到目前为止的错误代码:describe"#move"doit"sets@xand@yonestepforwardinthedirectiontherobotisfacing"dopoint.stub(:coords=).and_return([4,2])robot.moveexpect(robot

ruby-on-rails - rails : ActiveRecord and send; how do I set an activerecord instance's relation with only knowing the class names?

所以我遍历我所有的AR并动态设置它们的关系...所以我知道我有SomeObject并且它属于ManyObjects...我想做这样的事情:an_object.some_relation=related_objectan_object.save有没有办法通过发送或类似的方法来做到这一点?这当然行不通:an_object.send(some_relation_name,related_object)这行得通,我只是想用一种不那么危险、更像Rails-meta的方式来做:an_object.update_attributes({"#{some_relation_name}_id"=>rela

ruby-on-rails - 如何 : Model scope for todays records

嘿,如何将Rails3中的范围设置为今天的记录?这还行。我没有得到任何数据。classMyModel{:created_at=>Date.today}end 最佳答案 由于“created_at”列包含日期和时间,但您只需要比较日期,您有两种方法(我假设您使用的是MySQL):使用BETWEEN:scope:today,lambda{WHERE("created_atBETWEEN'#{DateTime.now.beginning_of_day}'AND'#{DateTime.now.end_of_day}'")}使用DATE()函

Ruby 未绑定(bind)方法 : Is it possible to force bind to instances of other classes?

我想知道我是否可以强制它发生classAdefbomb;"bomb";endendclassB;endbomb=A.instance_method(:bomb)b=B.newbomb.bind(b)当前它抛出错误TypeError:bindargumentmustbeaninstanceofA我发现这对我可以使用这些未绑定(bind)的方法所做的事情非常有限,可能性有点有限。在这样的情况下(而且我不仅仅指幂等函数)它会有意义吗?一个执行错误就足够了,以防我处理A中未在B中复制的变量。我真的很想知道如何强制执行此绑定(bind)。 最佳答案

ruby-on-rails - has_one,:through => model VS simple method?

我在使用has_one,through=>model时遇到一些问题。最好是向您展示我的案例。classCategoryhas_many:articlesendclassArticlehas_many:commentsbelongs_to:categoryendclassCommentbelongs_to:articlehas_one:category,:through=>:articlesend一切正常。我可以做comment.category。问题是当我创建新评论并设置其文章时,我必须保存评论以使关联有效。示例:>>comment=Comment.new>>comment.artic

去 : model ( or interface ) function with input of different types

现在我有一个用并行算法计算统计数据的小应用程序。现在我在扩展某些功能时遇到了问题。我会尽快解释。应用程序是建立在revel框架上的。“stat”Controller的一项操作采用传入的POSTjson。解析它。并为任务和结果生成两个channel(goroutines)。所有这一切都像一个魅力。但是我在模型方面遇到了麻烦。我编写了能够线性扩展模型数量的代码,但目前只有一个在工作。而且并不是所有的方法都用于这种扩展。在代码的某些部分我有这个:fort:=rangein{forsourceName,charts:=ranget.Request.Charts{varcacheData[]by

java - org.hibernate.TransientObjectException : object references an unsaved transient instance - save the transient instance before flushing

在我的项目中,我有User,Role,UserRole和BloodGroup实体。首先我拿List来自DB并设置为User.那我给User和Role实体到UserRole.之后我插入User到DB,然后我尝试插入UserRole,但我得到一个错误。当我查看数据库时,BloodGroup的ID未插入User表。如果我选择第一个BloodGroup在列表中,我得到一个错误。其他选项正常。我上网查了一下,发现cascade=CascadeType.ALL,但这会将相同的数据添加到BloodGroup,这意味着我有更多Arh+BloodGroup.实体:@Entity@Table(name="

java - org.hibernate.TransientObjectException : object references an unsaved transient instance - save the transient instance before flushing

在我的项目中,我有User,Role,UserRole和BloodGroup实体。首先我拿List来自DB并设置为User.那我给User和Role实体到UserRole.之后我插入User到DB,然后我尝试插入UserRole,但我得到一个错误。当我查看数据库时,BloodGroup的ID未插入User表。如果我选择第一个BloodGroup在列表中,我得到一个错误。其他选项正常。我上网查了一下,发现cascade=CascadeType.ALL,但这会将相同的数据添加到BloodGroup,这意味着我有更多Arh+BloodGroup.实体:@Entity@Table(name="