草庐IT

simulate_many

全部标签

ruby-on-rails - 通过 ID 数组的关联创建多个 has_many

我有模型User、Photo和Favorite,其中favorites是来自的连接表用户到照片,即:classUser假设@user是User的实例,photo_ids是照片的主键数组。将所有这些照片添加到@user.photos的最快和/或最简洁的方法是什么?我能做到的最好的是:@user.favorites.create(photo_ids.map{|id|{photo_id:id}})但这对我来说似乎很冗长。Rails没有更好的方法来处理这个问题吗?其他问题涉及通过嵌套表单创建多个has_many:through:关联,但我试图在JSONAPI中执行此操作,因此不涉及任何表单。

ruby-on-rails - rails 5 : Add record to join table without model (has_and_belongs_to_many)

我有一个User和MeetOption表。这是一个多对多关系,我能够使用create_join_table命令创建一个连接表:railsgmigrationCreateJoinTableUsersMeetOptions用户meet_options这生成了一个迁移文件:classCreateJoinTableUsersMeetOptions我还使用has_and_belongs_to_many创建了user和meet_option模型之间的关联classUserclassMeetOption关联工作正常,我可以在Rails控制台中查询例如user.meet_options。我的问题是:加

ruby-on-rails - 自引用 has_many :through with customized :primary key issue

我正在尝试在我的Rails2.3.8应用程序(ruby1.8.7)中模拟twitter模型classConnection'subject_id',:primary_key=>'user_id',:class_name=>'User'belongs_to:follower,:foreign_key=>'follower_id',:primary_key=>'user_id',:class_name=>'User'endclassUser'user_id',:foreign_key=>'follower_id',:class_name=>'Connection'has_many:relat

ruby-on-rails - has_many 关系的动态类名

我正在尝试使has_many与动态class_name属性建立关系classCategory(lambda{returnself.item_type})end或classCategoryself.item_typeend但是我有错误:can'tconvertProcintoString或undefinedmethod`item_type'for#编辑我有两种不同类型的广告LeaseAd,RentAd他们使用单表继承实现然后我将Category广告作为嵌套集。我想动态指定哪种类型的广告属于Category对象。感谢您的帮助! 最佳答案

ruby-on-rails - has_and_belongs_to_many 关联不起作用

我的RubyOnRails项目中的has_and_belongs_to_many关联有问题。这是我的模型:classStore这是我的连接表迁移:create_table"furnitures_stores",:id=>false,:force=>truedo|t|t.integer"furniture_id"t.integer"store_id"end然后我尝试用seed.rb插入一些值:Furniture.delete_allfurnitures=Furniture.create([{name:'aaaa1'}])Store.delete_allstoree=Store.creat

ruby-on-rails - Rails 3 查找所有关联记录 has_many :through

我想列出与某个特定类别和类(class)相关的所有帖子。我有:classPost:category_postshas_many:classroom_postshas_many:classrooms,:through=>:classroom_postsendclassCategory:category_postsendclassCategoryPost:classroom_postsendclassClassroomPost我想做这样的事Post.where(["category.id=?ANDclassroom.id=?",params[:category_id],params[:cl

ruby - Rails acts_as_paranoid 和 has_many :through

所以我正在使用rails3_acts_as_paranoidgem,并且在使用has_many:throughassociations控制范围时遇到了一些问题。例如#User.rbacts_as_paranoidhas_many:foldershas_many:files,:through=>:folders-#Folder.rbacts_as_paranoidbelongs_to:userhas_many:files,:dependent=>:destroy-#File.rbacts_as_paranoidbelongs_to:files现在让我们在users_controller.

ruby-on-rails - 在 has_many :through relationship, 上覆盖 ActiveRecord << 运算符以接受连接模型的数据

我有三个类:Person、Position和Directory。APersonhas_many:directories,:through=>:position.目录有_many:people,:through=>:position.个人和目录都有_many:positions。除了具有id、person_id和directory_id之外,Position模型还有一个或多个附加字段(例如,title)。我希望能够在每次将人员添加到Directory.people集合时向连接模型添加数据,例如标题字段。通常的directory=Directory.last#Let'sassumethat

ruby-on-rails - rails 模型 has_many :through associations

我正在努力解决我的人际关系,但我在使用关联时遇到了问题。所以我有三个模型Workout、Exercise和WorkoutExercise。一个锻炼应该有很多练习,一个锻炼应该有不同的锻炼,因此我写道:classWorkout:workout_exercisesendclassExercise:workout_exercisesendclassWorkoutExercise我正在运行一些测试,但是一旦我创建了一个锻炼、锻炼然后​​将它们加入到workout_exercise类中,这些测试就没有通过。它不会让我像这样访问锻炼中的练习:Workout.createExercise.creat

ruby-on-rails - ActiveRecord has_many 其中表 A 中的两列是表 B 中的主键

我有一个模型,Couple,它有两列,first_person_id和second_person_id和另一个模型,Person,其主键是person_id并且具有列name这是我想要的用法:#including'Person'modelforeagerloading,thisiscrucialformec=Couple.find(:all,:include=>:persons)[0]puts"#{c.first_person.name}and#{c.second_person.name}"那我该怎么做呢? 最佳答案 在Couple