Skip to content

Use the associated join and not get all of the items from the source as options #449

@markmcdonald51

Description

@markmcdonald51

Hi All,

Using AS again for another interesting project that requires some intricate associations to be rendered. I am using the Chosen library for my form_ui where it makes sense and for the most part everything works great. However I am noticing that when I set up and association that runs thru a join table it ends up fetching all of the values for from the sourcing model instead of just the ones in the joined model. When I use checkboxes as a form_ui, it will check the ones from the join table but list all the options from the source.

so for example:

class Udt < ActiveRecord::Base
has_many :udt_table_fields, dependent: :destroy #join table
has_many :udt_fields, through: :udt_table_fields # source the value from here
has_many :udt_forms, dependent: :destroy
end

class UdtForm < ActiveRecord::Base
belongs_to :udt
has_many :udt_fields, through: :udt
has_many :udt_form_fields, dependent: :destroy
end

class UdtTableField < ActiveRecord::Base
#acts_as_list
belongs_to :udt
belongs_to :udt_field
has_many :udt_fields, through: :udt
end

class UdtFormField < ActiveRecord::Base
belongs_to :udt_form
belongs_to :udt_field
has_many :udt_fields, through: :udt_form
end

These associations work as expected
[27] pry(main)> Udt.first.udt_fields.pluck(:field_name)
Udt Load (0.2ms) SELECT "udts".* FROM "udts" ORDER BY "udts"."id" ASC LIMIT 1
(0.1ms) SELECT "udt_fields"."field_name" FROM "udt_fields" INNER JOIN "udt_table_fields" ON "udt_fields"."id" = "udt_table_fields"."udt_field_id" WHERE "udt_table_fields"."udt_id" = ? [["udt_id", 1]]
=> ["record_id",
"protocol_id",
"pi_name",
...
"type_last_monitoring_visit",
"cmp_complexity",
"next_imv",
"comments"]

Which in turn returns the expected 18 fields, good.

The problem occurs when I used AS to render Udt.first.udt_fields it returns a select with all of the fields in the udt_fields table rather than just the ones from the associated join. I know this is the case as I see it in the log's sql:

Started GET "/admin/udt_forms/2/edit?udt_id=1&association=udt_forms&parent_scaffold=admin%2Fudts&udt_id=1&adapter=_list_inline_adapter" for 127.0.0.1 at 2015-10-29 14:18:30 -0400
Processing by Admin::UdtFormsController#edit as JS
Parameters: {"udt_id"=>"1", "association"=>"udt_forms", "parent_scaffold"=>"admin/udts", "adapter"=>"_list_inline_adapter", "id"=>"2"}
Udt Load (0.1ms) SELECT "udts".* FROM "udts" WHERE "udts"."id" = ? LIMIT 1 [["id", 1]]
UdtForm Load (0.1ms) SELECT "udt_forms".* FROM "udt_forms" WHERE "udt_forms"."udt_id" = ? AND ('t'='t') AND "udt_forms"."id" = ? LIMIT 1 [["udt_id", 1], ["id", 2]]
Rendered /home/mark/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/active_scaffold-c0a93658b329/app/views/active_scaffold_overrides/_form_messages.html.erb (0.9ms)
UdtFormField Load (0.1ms) SELECT "udt_form_fields".* FROM "udt_form_fields" WHERE "udt_form_fields"."udt_form_id" = ? [["udt_form_id", 2]]
Rendered /home/mark/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/active_scaffold-c0a93658b329/app/views/active_scaffold_overrides/_horizontal_subform_header.html.erb (0.9ms)
UdtField Load (1.4ms) SELECT "udt_fields".* FROM "udt_fields" WHERE "udt_fields"."id" = ? LIMIT 1 [["id", 16]]
UdtField Load (0.3ms) SELECT "udt_fields".* FROM "udt_fields" WHERE ('t'='t')
UdtField Load (7.2ms) SELECT "udt_fields".* FROM "udt_fields" WHERE "udt_fields"."id" = ? LIMIT 1 [["id", 5]]
UdtField Load (0.1ms) SELECT "udt_fields".* FROM "udt_fields" WHERE "udt_fields"."id" = ? LIMIT 1 [["id", 24]]
UdtField Load (3.0ms) SELECT "udt_fields".* FROM "udt_fields" WHERE "udt_fields"."id" = ? LIMIT 1 [["id", 50]]
Rendered /home/mark/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/active_scaffold-c0a93658b329/app/views/active_scaffold_overrides/_form_association_record.html.erb (78.3ms)

Is there a slick way to make it only used the associated join and not get all of the records from the source associated join? It is almost there, but for this particular screen the only items that can be selected are ones from the joining table.

Thanks in advance.
Mark

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions