Chaining Fields

34 views
Skip to first unread message

JB

unread,
Oct 15, 2011, 9:47:52 AM10/15/11
to ActiveScaffold : Ruby on Rails plugin
I have the following model:

class CostCode < ActiveRecord::Base
end

class SubContractor < ActiveRecord::Base
has_many :orders
belongs_to :cost_code
end

class Order < ActiveRecord::Base
belongs_to :sub_contractor
belongs_to :cost_code
end

The UI for Order.sub_contractor is a select box. When I select the
sub_contractor on the order, how can I chain the fields so that the
sub_contractor.cost_code is used to populate the order.cost_code?

Cláudio Forain

unread,
Oct 15, 2011, 9:59:56 AM10/15/11
to actives...@googlegroups.com
Maybe this will help:

https://github.com/activescaffold/active_scaffold/wiki/Chaining-Form-Fields

PS: Just out of curiosity, shouldn't your model be something like this?

class CostCode < ActiveRecord::Base
has_many :subcontractors
has_many :orders
end

> --
> You received this message because you are subscribed to the Google Groups "ActiveScaffold : Ruby on Rails plugin" group.
> To post to this group, send email to actives...@googlegroups.com.
> To unsubscribe from this group, send email to activescaffol...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/activescaffold?hl=en.
>
>

JB

unread,
Oct 15, 2011, 10:06:36 AM10/15/11
to ActiveScaffold : Ruby on Rails plugin
I looked at the wiki article and still couldn't seem to figure it out.

And no, my model really doesn't have the reverse association. The cost
codes are just a table that provides a bunch of values that can be
selected for a drop down list that is used in multiple places.

On Oct 15, 8:59 am, Cláudio Forain <claudiofor...@gmail.com> wrote:
> Maybe this will help:
>
> https://github.com/activescaffold/active_scaffold/wiki/Chaining-Form-...
>
> PS: Just out of curiosity, shouldn't your model be something like this?
>
> class CostCode < ActiveRecord::Base
>   has_many :subcontractors
>   has_many :orders
> end
>

Cláudio Forain

unread,
Oct 15, 2011, 10:36:49 AM10/15/11
to actives...@googlegroups.com
I see. I'm not an expert, but I will try to help :p
Considering your tables have these fields:

Order: subcontractor_id, costcode_id
Subcontractor: costcode_id

Well, here I would try to do something like this in options_helper.rb

module OrdersHelper
def options_for_association_conditions(association)
if association.name == :cost_code
["cost_codes.id" IN @record.costcode_id]
else
super
end
end

That worked around here, you should try.
end

Cláudio Forain

unread,
Oct 15, 2011, 10:38:17 AM10/15/11
to actives...@googlegroups.com
Sorry, my mistake:

module OrdersHelper
def options_for_association_conditions(association)
if association.name == :cost_code

["cost_codes.id" IN @record.subcontractor.costcode_id]
else
super
end
end

2011/10/15 Cláudio Forain <claudi...@gmail.com>:

JB

unread,
Oct 15, 2011, 11:04:52 AM10/15/11
to ActiveScaffold : Ruby on Rails plugin
Hmm . . . I'm no expert either, but that just throws an exception:

/Users/jeff/rails/reconn/app/helpers/orders_helper.rb:5: syntax error,
unexpected tCONSTANT, expecting ']'
["cost_codes.id" IN @record.costcode_id]
^
/Users/jeff/rails/reconn/app/helpers/orders_helper.rb:5: syntax error,
unexpected ']', expecting keyword_end

But I think you're missing my model. There are no cost_codes
association anywhere. The cost_codes table is an "admin" table. The
order and sub_contractor models have a cost_code_id column, but the
cost_code model does not have a foreign key. The idea is, when I
create a sub_contractor I assign a "default" cost_code to
sub_contractor.cost_code_id. Then, when I create an order and select a
sub_contractor, I want order.cost_code_id to equal the
sub_contractor.cost_code_id default. But a user can always come back
later to the order and assign a different cost_code_id to it. Does
that make sense?

On Oct 15, 9:36 am, Cláudio Forain <claudiofor...@gmail.com> wrote:
> I see. I'm not an expert, but I will try to help :p
> Considering your tables have these fields:
>
> Order:  subcontractor_id, costcode_id
> Subcontractor: costcode_id
>
> Well, here I would try to do something like this in options_helper.rb
>
> module OrdersHelper
>   def options_for_association_conditions(association)
>     if association.name == :cost_code
>       ["cost_codes.id"  IN @record.costcode_id]
>     else
>       super
>     end
>   end
>
> That worked around here, you should try.
> end
>

Cláudio Forain

unread,
Oct 15, 2011, 11:12:13 AM10/15/11
to actives...@googlegroups.com
Sorry, Im kinda tired, what I meant was

def options_for_association_conditions(association)
if association.name == :cost_code

["cost_codes.id" IN (?) , @record.subcontractor.costcode_id]
else
super
end

That will solve the error, but let me think further about your relationship.

Cláudio Forain

unread,
Oct 15, 2011, 11:24:02 AM10/15/11
to actives...@googlegroups.com
I think that what this code above would restrict the costcode options,
not define the default one. Maybe for this, you should use
after_render_field as seen here:

https://github.com/activescaffold/active_scaffold/wiki/Chaining-Form-Fields

I have never done that, it's just a shot.


2011/10/15 Cláudio Forain <claudi...@gmail.com>:

JB

unread,
Oct 15, 2011, 12:57:20 PM10/15/11
to ActiveScaffold : Ruby on Rails plugin
That makes more sense. This is what I have, but it still doesn't work:

class OrdersController < ApplicationController
before_filter :authenticate_user!

active_scaffold :order do |conf|
conf.columns[:sub_contractor].form_ui = :select
conf.columns[:cost_code].form_ui = :select
conf.columns[:sub_contractor].update_column = :cost_code
end

protected

def after_render_field(record, column)
if column.name == :sub_contractor
record.cost_code = record.sub_contractor.cost_code
end
end
end

Somehow I need to re-render the cost_code select box. I'm making
progress though, so thanks.

Cláudio Forain

unread,
Oct 15, 2011, 1:05:30 PM10/15/11
to actives...@googlegroups.com
I think I have little to do with your progress, but it's nice to help
to brainstorm. Anyway, could you answer me some questions?

The code in the helper I told you to use worked for filtering the
select box? (even though I know it's not what you want)

Whats the result of yout after_render_field ?

Cláudio Forain

unread,
Oct 15, 2011, 1:41:52 PM10/15/11
to actives...@googlegroups.com
I don't know if that helps, but if you can somehow link this
after_render with a form override
(https://github.com/activescaffold/active_scaffold/wiki/Form-Overrides
or https://github.com/activescaffold/active_scaffold/wiki/Subform-Overrides),
maybe you can do this.

I was thinking something like triggering the form_override with the
after_render_field and do something that would be like passing a
html_option to the form

config.column[:cost_code].options = {:selected =>
record.sub_contractor.cost_code}

Maybe it's a long shot, but it's all I can think now.
2011/10/15 Cláudio Forain <claudi...@gmail.com>:

clyfe

unread,
Oct 17, 2011, 10:45:09 AM10/17/11
to actives...@googlegroups.com
Note that the new API is update_columns = [:col1, col2] (plural) instead update_column (singular)

JB

unread,
Oct 19, 2011, 11:20:23 AM10/19/11
to ActiveScaffold : Ruby on Rails plugin
Thanks. It looks like that was the problem. Works great now.
Reply all
Reply to author
Forward
0 new messages