草庐IT

ruby-on-rails - 通过关系在 has_many 中查找唯一记录的更好方法

在我的Rails应用程序中,用户可以参加事件-用户.rbhas_many:attendanceshas_many:events,through::attendances事件.rbhas_many:attendanceshas_many:users,through::attendances...有一个由event_id、user_id和其他一些零碎组成的考勤表-考勤.rbbelongs_to:userbelongs_to:writers_event在寻找特定的出勤率时,我发现自己使用的是.where....first-例如attendance=Attendance.where(user_

ruby - 基本 ruby 数组代码 : Testing if an array has a specific integer

我正在尝试测试数组中是否包含特定整数。现在我正在使用这个测试;defadmin?current_user.role_ids==[1,2]||current_user.role_ids==[2]||current_user.role_ids==[1,2,5]end代码有效,但我更愿意只测试整数“2”,而不是明确写出包含“2”的所有可能的数字组合。如果您有任何ruby建议,我将不胜感激。这是我在飞行中所能想象到的最好的结果。谢谢! 最佳答案 您是否在寻找Array#include??current_user.role_ids.inclu

ruby-on-rails - ruby rails 3 : How to access an attribute of each record in a has_many query

我不确定如何在标题中表达它,但我正在尝试执行以下操作。我有2个具有以下关系的模型:classCampaign现在,Point模型有一个“locale”属性,我希望能够将特定事件的每个点的所有“locale”放入一个数组、集合或其他任何东西中,而无需执行类似locales=Array.newcampaign.points.eachdo|p|locales我在想一些与campaign.points.locales类似的东西。是否有一些很好的Rails方法来执行此查询,或者我只需要遍历集合?感谢您的帮助。编辑:还因为这种关系实际上是一种“has_manythrough”关系,所以这种方法不起

ruby-on-rails - Rails - activeadmin,在更新 "parent"记录时复制 has_many 记录

我的模型如下:classProject这是activeadmin文件:ActiveAdmin.registerProjectdoremove_filter:projects_sectorspermit_params:title,:info,:case_study,project_images_attributes:[:image,:cover]indexdocolumn:titleactionsendform:html=>{:enctype=>"multipart/form-data"}do|f|f.inputs"Project"dof.input:titlef.input:infof

ruby-on-rails - Ruby On Rails Active Admin has_many 更改下拉列表以使用不同的列

我是ActiveAdmin和RoR的新手,我不知道如何更改has_many关联中下拉菜单的可见值。填充模型classFillup汽车模型classCartruekeyisfillup_id:integerhas_many:fillupsend当前显示的内容:它目前显示im假定对分配给它的Car的编码引用。我需要它展示什么:我需要它来显示在CarModel中定义为description:string的描述。 最佳答案 像这样的东西应该有用...在app/admin/model_name.rbformdo|f|f.inputs"MyMo

ruby-on-rails - 从 has_many :through join models in Rails 中优雅地选择属性

我想知道从has_many中的连接模型中选择属性的最简单/最优雅的方法是什么:通过关联。假设我们有具有以下Item类的Items、Catalogs和CatalogItems:classItem:catalog_itemsend此外,假设CatalogueItems有一个位置属性,并且在任何目录和任何项目之间只有一个CatalogueItem。检索位置属性的最明显但有点令人沮丧的方法是:@item=Item.find(4)@catalog=@item.catalogs.first@cat_item=@item.catalog_items.first(:conditions=>{:cata

ruby-on-rails - ActiveRecord has_many 通过与 arg 的作用域关联

我已经看到许多问题的答案,这些问题解决了如何在ActiveRecord关联中使用范围block,包括将对象本身传递到block中,例如...classPatron(patron){baz:patron.blah},foreign_key::somekey,primary_key::somekeyendclassBar(bar){blah:bar.baz},foreign_key::somekey,primary_key::somekeyend这里使用显式主键和外键是由于底层表之间的遗留关系。生产系统中有数以亿计的“赞助人”。作为对@TJR的澄清-Patron和Bar之间的关系实际上是字

ruby - Mongoid has_and_belongs_to_many 协会

我试图让mongoid保存关联,但我只能让一侧工作。如果我有以下测试。test"shouldaddauserasafollowerwhenauserfollowsthegroup"do@cali_group.followers=[]@user1.followed_groups这是失败的,因为@cali_group.followers是[]。我已经用这个工作了一段时间,试过@cali_group.reload.但看起来在我的代码中执行此操作的唯一方法是处理连接的两端,即@cali_group.followers.如果需要,我可以在我的代码中这样做。polco_group和用户的模型在这里

ruby-on-rails - Active Admin - 在成员操作中使用 Form DSL(使用 has_many)

有没有办法从自定义成员操作中使用ActiveAdmin的表单dsl?我想保留has_many语义以避免必须自己从头开始实现它,但我想要一个单独的表单View。像这样的东西是理想的:member_action:subject,method::getdo@subject=Subject.find(params[:id])formdo|f|f.inputsdof.input:name,:required=>true,:input_html=>{:class=>"large"}endendend 最佳答案 Arbre似乎不支持formtas

ruby - 为什么我的 has_many through 关联记录(有时)是只读的?

我有三个ActiveRecord模型:Partner、MembershipChannel(这是一个STI模型,继承自Channel)和ChannelMembership(我不负责命名这些模型……)当我通过合作伙伴协会加载ChannelMembership时,我有时(!)以只读记录结束。这是在Rails3.0.9中。相同的代码在2.3.11中没有这种行为。>p=Partner.first>p.channel_memberships.map(&:readonly?)#=>[false,false,false,false,false,false]>p.reload.channel_membe