Tag: association

Ruby on Rails 3:结合多个has_many或has_many_through关联的结果

我有以下型号。 用户有UserActions,一个可能的UserAction可以是ContactAction(UserAction是一个多态)。 还有LoginAction等其他动作。所以 class User “ContactAction” has_many:user_actions has_many_polymorphs:user_actionables,:from => [:contact_actions,…],:through =>:user_actions 结束 class UserAction true 结束 class ContactAction <AR :: Base belongs_to:用户 named_scope:pending,… named_scope:active,… 结束 这个想法是一个ContactAction加入两个用户(在app中有其他后果),并且总是有一个接收端和一个发送端。 同时,ContactAction可以具有不同的状态,例如过期,待定等。 我可以说@user.contact_actions.pending或@user.contact_requests.expired列出用户发送或接收的所有待处理/过期请求。 这很好用。 我现在想要的是一种加入两种类型的ContactAction的方法。 即@user.contact_actions_or_requests 。 我尝试了以下方法: 类用户 def contact_actions_or_requests self.contact_actions + self.contact_requests 结束 # 要么 has_many:contact_actions_or_requests,:finder_sql => …,:counter_sql => … 结束 但是所有这些都存在这样的问题:在关联之上不可能使用额外的finder或named_scope,例如@user.contact_actions_or_requests.find(…)或@user.contact_actions_or_requests.expired 。 基本上,我需要一种表达1:n关联的方法,它有两条不同的路径。 一个是User -> ContactAction.user_id ,另一个是User […]

Rails – 检查has_many关联中是否存在记录

我不确定我的问题措辞是否正确。 我有三个模型: User , Item和UserItem 。 user has_many :user_items user has_many :items, through :user_items item has_many :user_items item has_many :users -> {uniq}, through :user_items item belongs_to :user user_item belongs_to :user user_item belongs_to :item 我需要一种方法来查看用户是否有一个项目来制作我的项目视图中的语句但是这里是catch,user_items具有enum status: [ :pending, approved] 。 所以我需要查看current_user是否具有某个:pending项。 例如,当用户访问item1的视图页面时,我有item_controller的show动作声明@item = Item.find_by_id(params[:id]) 。 但是,我可以用这个@item来查看用户是否有这个项目?

如何在rails中创建具有外键别名的灯具?

我有两个模型, App和User ,其中App有一个创建者是User 。 # app.rb class App < ActiveRecord::Base belongs_to :creator, class_name: 'User' end # user.rb class User < ActiveRecord::Base has_many :apps, foreign_key: "creator_id" end 我如何为此创建灯具? 我试过了: # apps.yml myapp: name: MyApp creator: admin (User) # users.yml admin: name: admin 但这不起作用,因为关系是别名外键,而不是多态类型。 省略创建者行中的(User)也不起作用。 我已经看过几个关于外键和灯具的线程,但没有一个真正回应这个。 (许多人建议使用factory_girl或机械师或固定装置的其他替代品,但后来我在其他地方看到他们有类似或其他问题)。

多对多连接和关联

用户可以通过Clips Scaffold上传Clips Scaffold ,此剪辑属于Show和/或Album (显示控制器,相册控制器)。 用户可以拥有 只有一个节目 很多ALBUMS 我想要完成的事情: 用户可以上传剪辑 ,这些剪辑可以附加到“ 显示”或/和“ 相册” 有关更详细的概述,请查看下图: 问题#1我不理解的是我必须使用的关联。 我目前的型号: 用户模型 : has_many :clips, dependent: :destroy has_one :show, dependent: :destroy has_many :albums, dependent :destroy 剪辑型号 : belongs_to :user 显示模型 : belongs_to :user 专辑模型 : belongs_to :user ——————————————————————————— 问题#2我不确定如何完成,当用户上传剪辑时,他可以将它附加(或分配)给一个节目或专辑。 我对Show做了什么(因为只有一个)只是在View页面上调用。 这意味着用户上传的每个剪辑都会出现在Show上。 哪个错了…… 现在我有一个Show和一个专辑我必须让用户能够选择在哪里附加它(在他拥有的许多专辑之一,或他的节目)。 或者我应该使用一个解决方案,用户可以通过相册或显示控制器上传剪辑(而不是通过单独的剪辑控制器 ) 我真的需要一些帮助:)

Rails根据父模型属性过滤子模型的记录

以下是1对M型号: class FotoGossip < ActiveRecord::Base has_many :uploads attr_accessible :published_at, … end class Upload < ActiveRecord::Base belongs_to :foto_gossip end 现在我想要Uploads.all的条件:published_at NOT NULL相应的上传的父模型?

Rails属于通过关联

我正在尝试将新模型添加到现有模型网格中。 现有的工作完美,但我不能让新工作正常工作,我想知道协会是否能够以我试图使其工作的方式工作。 更新:我刚刚被问到: belongs_to through是我在阅读这个问题时读过的东西。 如果它不存在,那么has_one through是否正确? 我也试过了,但也没用。 这是现有的网格: class Course has_many :users, through: :enrollments has_many :enrollments end class User has_many :courses, through: :enrollments has_many :enrollments end class Enrollment belongs_to :course belongs_to :user # has fields :user_id, :course_id end 现在,用户应该能够对他完成的课程进行评分。 (如果有的话,有一个他的身份证和课程ID的注册。)我认为最好写如下: class Course has_many :users, through: :enrollments has_many :enrollments has_many :ratings, through: :enrollments end class […]

在Rails查询中获取关联模型的属性

在我的rails应用程序中, 集合有许多项目 ,项目有很多步骤 。 我想抓住集合项目中所有步骤的ID,我想知道我是否可以在一个查询中完成所有操作。 例如,我知道我可以做以下事情 step_ids = [] @collection.projects.each do |project| project.steps.each do |step| step_ids << step.id end end 但是可以做以下事情: @collection.projects.include(:steps).pluck(“step.id”) //这里的语法不正确

为什么这个rails关联在急切加载后单独加载?

我试图通过急切加载避免N + 1查询问题,但它无法正常工作。 相关模型仍在单独加载。 以下是相关的ActiveRecords及其关系: class Player < ActiveRecord::Base has_one :tableau end Class Tableau :tableau_cards end Class TableauCard :card end class DeckCard :tableau_cards end class Card < ActiveRecord::Base has_many :deck_cards end class Turn < ActiveRecord::Base belongs_to :game end 我正在使用的查询是在Player的这个方法中: def tableau_contains(card_id) self.tableau.tableau_cards = TableauCard.find :all, :include => [ {:deck_card => (:card)}], :conditions => [‘tableau_cards.tableau_id = […]

Rails has_many通过使用source和source_type别名来处理多种类型

所以这是一个示例类 class Company < ActiveRecord::Base has_many :investments has_many :vc_firms, through: :investments, source: :investor, source_type: 'VentureFirm' has_many :angels, through: :investments, source: :investor, source_type: 'Person' end @ company.angels和@ company.vc_firms按预期工作。 但是,我如何拥有由两种源类型组成的@ company.investors? 这适用于投资表中投资者专栏的所有多态? 或者也许是使用范围合并所有source_type的方法? 投资模式如下: class Investment < ActiveRecord::Base belongs_to :investor, polymorphic: true belongs_to :company validates :funding_series, presence: true #, uniqueness: {scope: :company} validates :funded_year, presence: true, numericality: […]

belongs_to with:class_name选项失败

我不知道出了什么问题,但我无法使用belongs_to:class_name选项。 有人可以启发我吗? 非常感谢! 这是我的代码中的一个剪辑。 class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.text :name end end def self.down drop_table :users end end ##################################################### class CreateBooks false end end def self.down drop_table :books end end ##################################################### class User < ActiveRecord::Base has_many: books end ##################################################### class Book ‘User’, :validate => true end ##################################################### class BooksController […]