【问题标题】:Collection_select - Syntax to display parent field data in child queryCollection_select - 在子查询中显示父字段数据的语法
【发布时间】:2014-01-16 07:19:57
【问题描述】:

我希望在这里得到一些很大的帮助。我在 Rails 3.2 的许多领域都是菜鸟,但我正在变得更好。

我有一个已检索到正确记录的集合选择。我有多个属性,然后有多个用户。集合选择正确地使用 UserProperty 表仅过滤属性用户(我希望集合显示与该属性相关的每个用户)。所以我有用户 ID 而不是名字。

所以我尝试通过查询父表(用户)来创建一个对象以提取父属性并通过。不幸的是,第二个查询只传递了一条记录。所以放弃了。

最简单的方法是在 collection_select 中使用第一个查询,然后根据参考用户 ID 显示父字段。是否有允许我在集合选择和查询对象“池”中将文本显示回父 (USER) 表的语法?

目标是通过嵌套路由(@property)查询 UserProperties - 工作。 (@pool 包含正确的结果)

然后显示来自用户的名称字段。 (用户名)

这是我的代码:

控制器:

      def find_property

        @property = Property.find(params[:property_id])

        @pool = UserProperty.find_all_by_property_id(@property) 

      end

查看:

    <%= fields_for (:assigned_to_user_id) do |pool| %>
        <%= pool.collection_select(:assigned_to_user_id, @pool, :id, :id  ) %>
        <!-- Need to change last field from id to name field of Users Table -->

模型设计:(需要用户名)

    # Table name: users
    #
    #  id              :integer          not null, primary key
    #  name            :string(255)
    #  email           :string(255)

    class User < ActiveRecord::Base
      attr_accessible :name, :email

      has_many :properties, through: :user_properties
    end

    # Table name: user_properties
    #
    #  id          :integer          not null, primary key
    #  user_id     :integer
    #  property_id :integer

    class UserProperty < ActiveRecord::Base
      attr_accessible :property_id, :user_id

      belongs_to :user
      belongs_to :property
    end

【问题讨论】:

  • 不要在标题中写“已解决”。相反,使用旁边的大复选标记按钮将可接受的答案标记为已接受。

标签: ruby-on-rails ruby-on-rails-3.2 parent-child grouped-collection-select


【解决方案1】:

问题已解决。 Rails 内置了基于嵌套路由的过滤器。 因此,一位朋友按照 RoyTheBoy 的建议,使用更简单的方法帮助我解决了这个问题。

虽然存在 many through 关系,但需要一个 has many 语句。

型号如下:

    has_many :users, through: :user_properties
    has_many :user_properties  (new line)

然后从@pool中删除过滤器

     @pool = @property.users.all  (rails automagically pulls from the nested route)

仅供参考 - 用于加载嵌套属性的现有 before_filter

    @property = Property.find(params[:property_id])

这允许更简单的 collection_select

    <%= fields_for (:assigned_to_user_id) do |pool| %>
    <%= collection_select(:assigned_to_user_id, :user_id, @pool , :id, :name, prompt: true) %>

属性的嵌套路由,自动处理过滤!一定要喜欢rails,尤其是像我这样的新手! 感谢罗伯特的帮助!

【讨论】:

    【解决方案2】:

    根据您的表定义,在您需要的模型中

    class UserPropery < ActiveRecord::Base
    belongs_to :user
    
    class User < ActiveRecord::Base
    has_many :user_properties
    

    你可以使用

    UserProperty.where(...).user.name
    

    不管你怎么说 “我有多个属性,然后有多个用户。”

    这对我来说意味着一个属性可以有很多用户,在这种情况下,您的用户表和模型中应该有一个 user_propery_id 字段

    class UserPropery < ActiveRecord::Base
    has_many :user
    
    class User < ActiveRecord::Base
    belongs_to :user_properties
    

    然后你可以使用类似的东西

    UserProperty.where(...).users.collect {|user| [ user.id, user.name ]} 
    

    这是对关联如何运作的一个很好的解释 http://guides.rubyonrails.org/association_basics.html

    【讨论】:

    • 感谢您的回复。我原本打算给 Users 模型一个 property_id 字段,但应用程序要求一个 User 有多个属性。这通过一个额外的表 UserProperties 解决了,其中一个用户被分配给一个或多个属性。现在的关系很好并且可以正确填充。这就是为什么我可以在其中 Property_Id = 嵌套地址(@property)的 UserProperties 上拉出 collection_select。我不认为关系是问题,已经有更复杂的关系可以正常运行。再次感谢。
    • 为清楚起见,我在代码部分添加了关系信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 2013-03-25
    相关资源
    最近更新 更多