Updating Multiple Fields

6 views
Skip to first unread message

HangingClowns

unread,
Aug 7, 2009, 6:37:22 PM8/7/09
to ActiveScaffold : Ruby on Rails plugin
I have scoured the internet all day to find the solution to my
problem. How to have one select field get updated by another select
field. I found this link that helped:
http://wiki.github.com/activescaffold/active_scaffold/chaining-form-fields

However, I need to be able to update 3 fields, not 1. How can I pass
in an array of the fields and perform the updating? I tried to do a
recursion, but it didn't understand what the symbol was....

module DefaultAudioHelper
def options_for_association_conditions(association)
if association.name == :speaker_test
{'speaker_tests.phone_id' => @record.phone_id}
options_for_association_conditions(:audio_out_test_half)
elsif association.name == :audio_out_test_half
{'audio_out_tests.phone_id' => @record.phone_id}
else
super
end
end
end

Can someone point me in the right direction, please? :)

Thanks!

Sergio Cambra .:: entreCables S.L. ::.

unread,
Aug 10, 2009, 4:23:41 AM8/10/09
to actives...@googlegroups.com
On Sábado, 8 de Agosto de 2009 00:37:22 HangingClowns escribió:
> I have scoured the internet all day to find the solution to my
> problem. How to have one select field get updated by another select
> field. I found this link that helped:
> http://wiki.github.com/activescaffold/active_scaffold/chaining-form-fields
>
> However, I need to be able to update 3 fields, not 1. How can I pass
> in an array of the fields and perform the updating? I tried to do a
> recursion, but it didn't understand what the symbol was....
>
> module DefaultAudioHelper
> def options_for_association_conditions(association)
> if association.name == :speaker_test
> {'speaker_tests.phone_id' => @record.phone_id}
> options_for_association_conditions(:audio_out_test_half)
Remove this line, options for association must be the conditions part for a
find method.
> elsif association.name == :audio_out_test_half
> {'audio_out_tests.phone_id' => @record.phone_id}
> else
> super
> end
> end
> end

What you need is to update two fields when first select is updated, and one field
when second select is updated. Currently you can chain field A with field B, and
chain field B with C, but when you change field A, only field B was updated, field
C was only updated when you edited field B. I have added support for this, so
when you change a field (A), the update_column field is updated (B), if that
field has a chained field (C) it's updated too, and so on.

>
> Can someone point me in the right direction, please? :)
>
> Thanks!
>
>
--
Sergio Cambra .:: entreCables S.L. ::.
Mariana Pineda 23, 50.018 Zaragoza
T) 902 021 404 F) 976 52 98 07 E) ser...@entrecables.com

G. Sobrinho

unread,
Aug 10, 2009, 9:51:26 AM8/10/09
to actives...@googlegroups.com
Absolute fantatisc! I wanted this some times and now have support. Really thanks Sergio!

2009/8/10 Sergio Cambra .:: entreCables S.L. ::. <ser...@entrecables.com>



--
Cordialmente,

Gabriel Sobrinho
Diretor de desenvolvimento

Hite - Comunicação Digital e Mídia Interativa
http://www.hite.com.br/

+55 31 8775 8378

Allen Wyma

unread,
Aug 17, 2009, 3:14:26 PM8/17/09
to actives...@googlegroups.com
Sorry for the late reply, but I got caught up in another Rails project I've been working on. I'm still not following you completely. let me show you what I wrote for code, and maybe you can follow how it's not "chaining" right for me.


module DefaultAudioHelper
  def options_for_association_conditions(association)
    if association.name == :speaker_test
      {'speaker_tests.phone_id' => @record.phone_id}
    elsif association.name == :audio_out_test_half
      {'audio_out_tests.phone_id' => 'speaker_tests.phone_id'}
    elsif association.name == :audio_out_test_full
      {'audio_out_tests.phone_id' => 'speaker_tests.phone_id'}

    else
      super
    end
  end
end

class DefaultAudioController < ApplicationController
  before_filter :admin_authorize
  layout 'welcome'
  active_scaffold do |config|
    config.label = "Default Audio"
    config.columns[:audio_out_test_half].label = "Audio Out Test (Half)"
    config.columns[:audio_out_test_half].search_sql = "half=1"
    config.columns[:audio_out_test_full].label = "Audio Out Test (Full)"
    config.columns[:audio_out_test_full].search_sql = "half=0"
    config.columns[:speaker_test].label = "Speaker Test"
    config.columns[:phone].form_ui = :select
    config.columns[:phone].options = {:update_column => :speaker_test}
    config.columns[:speaker_test].form_ui = :select
    config.columns[:speaker_test].options = {:update_column => :audio_out_test_half}
    config.columns[:audio_out_test_half].form_ui = :select
    config.columns[:audio_out_test_half].options = {:update_column => :audio_out_test_full}
    config.columns[:audio_out_test_full].form_ui = :select
  end
end

Audio Out for half and full are the same table, and only difference between the 2 is there is a boolean true or false for being half or not, as you can see  in my search_sql. I'm running MySQL on a WinXP (Not by choice :p).

Thanks for any help you can give me.

Sergio Cambra .:: entreCables S.L. ::.

unread,
Aug 18, 2009, 4:27:34 AM8/18/09
to actives...@googlegroups.com
Do you get some errors in your log? Is it working at least first level
chaining? Do you get a request when you change phone select?

Allen Wyma

unread,
Aug 18, 2009, 8:10:29 AM8/18/09
to actives...@googlegroups.com
I don't get errors in my log, just exactly what I put in. The first part works just great, and I can even see the ID that's getting passed into the Speaker Tests table, and that gets updated instantly... The other 2 tables don't. Here's a log:

dapter=_list_inline_adapter]
  [4;36;1mSQL (0.0ms) [0m   [0;1mSET NAMES 'utf8' [0m
  [4;35;1mSQL (0.0ms) [0m   [0mSET SQL_AUTO_IS_NULL=0 [0m
  [4;36;1mDefaultAudio Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `default_audio` [0m
  [4;35;1mAudioOutTest Columns (15.0ms) [0m   [0mSHOW FIELDS FROM `audio_out_tests` [0m
  [4;36;1mPhone Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `phones` [0m
  [4;35;1mFormFactor Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `form_factors` [0m
  [4;36;1mManufacturer Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `manufacturers` [0m
  [4;35;1mTechnology Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `technologies` [0m
  [4;36;1mSpeakerTest Columns (16.0ms) [0m   [0;1mSHOW FIELDS FROM `speaker_tests` [0m


Processing DefaultAudioController#render_field (for 127.0.0.1 at 2009-08-18 07:09:36) [GET]
  Parameters: {"column"=>"phone", "value"=>"1"}
  [4;35;1mUser Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `users` [0m
  [4;36;1mUser Load (0.0ms) [0m   [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1 [0m
  [4;35;1mCACHE (0.0ms) [0m   [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1 [0m
  [4;36;1mPhone Load (0.0ms) [0m   [0;1mSELECT * FROM `phones` WHERE (`phones`.`id` = 1) [0m
Rendering render_field
  [4;35;1mSpeakerTest Load (0.0ms) [0m   [0mSELECT * FROM `speaker_tests` WHERE ((`speaker_tests`.`phone_id` = 1)) [0m
  [4;36;1mCACHE (0.0ms) [0m   [0;1mSELECT * FROM `phones` WHERE (`phones`.`id` = 1) [0m
Rendered _form_attribute (16.0ms)
Completed in 141ms (View: 63, DB: 31) | 200 OK [http://127.0.0.1/default_audio/render_field?column=phone&value=1]
column=phone&value=1]
  [4;35;1mSQL (0.0ms) [0m   [0mSET NAMES 'utf8' [0m
  [4;36;1mSQL (0.0ms) [0m   [0;1mSET SQL_AUTO_IS_NULL=0 [0m
  [4;35;1mDefaultAudio Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `default_audio` [0m
  [4;36;1mAudioOutTest Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `audio_out_tests` [0m
  [4;35;1mPhone Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `phones` [0m
  [4;36;1mFormFactor Columns (15.0ms) [0m   [0;1mSHOW FIELDS FROM `form_factors` [0m
  [4;35;1mManufacturer Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `manufacturers` [0m
  [4;36;1mTechnology Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `technologies` [0m
  [4;35;1mSpeakerTest Columns (16.0ms) [0m   [0mSHOW FIELDS FROM `speaker_tests` [0m


Processing DefaultAudioController#render_field (for 127.0.0.1 at 2009-08-18 07:09:39) [GET]
  Parameters: {"column"=>"speaker_test", "value"=>"1"}
  [4;36;1mUser Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `users` [0m
  [4;35;1mUser Load (0.0ms) [0m   [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1 [0m
  [4;36;1mCACHE (0.0ms) [0m   [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1 [0m
  [4;35;1mSpeakerTest Load (0.0ms) [0m   [0mSELECT * FROM `speaker_tests` WHERE (`speaker_tests`.`id` = 1) [0m
Rendering render_field
  [4;36;1mAudioOutTest Load (0.0ms) [0m   [0;1mSELECT * FROM `audio_out_tests` WHERE ((`audio_out_tests`.`phone_id` = 'speaker_tests.phone_id')) [0m
Rendered _form_attribute (0.0ms)
Completed in 78ms (View: 46, DB: 31) | 200 OK [http://127.0.0.1/default_audio/render_field?column=speaker_test&value=1]

Thanks,
Allen

Sergio Cambra .:: entreCables S.L. ::.

unread,
Aug 18, 2009, 8:43:02 AM8/18/09
to actives...@googlegroups.com
On Martes, 18 de Agosto de 2009 14:10:29 Allen Wyma escribió:
> I don't get errors in my log, just exactly what I put in. The first part
> works just great, and I can even see the ID that's getting passed into the
> Speaker Tests table, and that gets updated instantly... The other 2 tables
> don't. Here's a log:

I don't know what you want to get in audio_out_test_half and
audio_out_test_full, but the sql is wrong, it's searching audio_out_tests with
phone_id equals to a string ('speaker_tests.phone_id')
SELECT * FROM `audio_out_tests` WHERE ((`audio_out_tests`.`phone_id` =
'speaker_tests.phone_id'))

You have to change options_for_association_conditions, and put
@record.speaker_test.phone_id instead of 'speaker_tests.phone_id'

def options_for_association_conditions(association)
if association.name == :speaker_test
{:phone_id => @record.phone_id}
elsif association.name == :audio_out_test_half
{:phone_id => @record.speaker_test.phone_id}
elsif association.name == :audio_out_test_full
{:phone_id => @record.speaker_test.phone_id}
else
super
end
end

Allen Wyma

unread,
Aug 18, 2009, 8:45:49 AM8/18/09
to actives...@googlegroups.com
Why can't I just keep using @record.phone_id ? For some reason, it keeps coming in as NULL? Why is that?

Sergio Cambra .:: entreCables S.L. ::.

unread,
Aug 18, 2009, 8:50:10 AM8/18/09
to actives...@googlegroups.com
On Martes, 18 de Agosto de 2009 14:45:49 Allen Wyma escribió:
> Why can't I just keep using @record.phone_id ? For some reason, it keeps
> coming in as NULL? Why is that?

render_field use a new blank record with only the recently changed field with a
value

If you set update_column to speaker_test column, when you change the
speaker_test field, an AJAX request with the new value for speaker_test is made
(only with params column=speaker_test and value=new_value). So you can filter
using only that column.

It's a basic support for chaining fields, if you need something more advanced
you will have to override it for each case.

Allen Wyma

unread,
Aug 18, 2009, 9:41:02 AM8/18/09
to actives...@googlegroups.com
Still not working for me. Gives me a HUGE error when I changed it to this AND tried to just create it a record. It would not even generate the create part.:


module DefaultAudioHelper
  def options_for_association_conditions(association)
    if association.name == :speaker_test
      {'speaker_tests.phone_id' => @record.phone_id}
    elsif association.name == :audio_out_test_half
      {'audio_out_tests.phone_id' => @record.speaker_test.phone_id}
    elsif association.name == :audio_out_test_full

      {'audio_out_tests.phone_id' => @record.phone_id}
    else
      super
    end
  end
end

Console Log:
escues/layout (internal_server_error)

  [4;36;1mSQL (0.0ms) [0m   [0;1mSET NAMES 'utf8' [0m
  [4;35;1mSQL (0.0ms) [0m   [0mSET SQL_AUTO_IS_NULL=0 [0m
  [4;36;1mDefaultAudio Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `default_audio` [0m
  [4;35;1mAudioOutTest Columns (15.0ms) [0m   [0mSHOW FIELDS FROM `audio_out_tests` [0m
  [4;36;1mPhone Columns (16.0ms) [0m   [0;1mSHOW FIELDS FROM `phones` [0m

  [4;35;1mFormFactor Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `form_factors` [0m
  [4;36;1mManufacturer Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `manufacturers` [0m
  [4;35;1mTechnology Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `technologies` [0m
  [4;36;1mSpeakerTest Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `speaker_tests` [0m


Processing DefaultAudioController#new (for 127.0.0.1 at 2009-08-18 08:35:11) [GET]
  Parameters: {"adapter"=>"_list_inline_adapter"}

  [4;35;1mUser Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `users` [0m
  [4;36;1mUser Load (0.0ms) [0m   [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1 [0m
  [4;35;1mCACHE (0.0ms) [0m   [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1 [0m
  [4;36;1mAudioOutTest Load (0.0ms) [0m   [0;1mSELECT * FROM `audio_out_tests` WHERE ((`audio_out_tests`.`phone_id` IS NULL)) [0m
Rendered _form_attribute (15.0ms)

Also tried what you sent to me and it also errors out on Create:


module DefaultAudioHelper
  def options_for_association_conditions(association)
    if association.name == :speaker_test
      {:phone_id => @record.phone_id}
    elsif association.name == :audio_out_test_half
      {:phone_id => @record.speaker_test.phone_id}
    elsif association.name == :audio_out_test_full
      {:phone_id => @record.phone_id}

    else
      super
    end
  end
end

Console Log:
escues/layout (internal_server_error)

  [4;36;1mSQL (0.0ms) [0m   [0;1mSET NAMES 'utf8' [0m
  [4;35;1mSQL (0.0ms) [0m   [0mSET SQL_AUTO_IS_NULL=0 [0m
  [4;36;1mDefaultAudio Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `default_audio` [0m
  [4;35;1mAudioOutTest Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `audio_out_tests` [0m

  [4;36;1mPhone Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `phones` [0m
  [4;35;1mFormFactor Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `form_factors` [0m
  [4;36;1mManufacturer Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `manufacturers` [0m
  [4;35;1mTechnology Columns (16.0ms) [0m   [0mSHOW FIELDS FROM `technologies` [0m

  [4;36;1mSpeakerTest Columns (16.0ms) [0m   [0;1mSHOW FIELDS FROM `speaker_tests` [0m


Processing DefaultAudioController#new (for 127.0.0.1 at 2009-08-18 08:40:37) [GET]
  Parameters: {"adapter"=>"_list_inline_adapter"}
  [4;35;1mUser Columns (15.0ms) [0m   [0mSHOW FIELDS FROM `users` [0m

  [4;36;1mUser Load (0.0ms) [0m   [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1 [0m
  [4;35;1mCACHE (0.0ms) [0m   [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1 [0m
  [4;36;1mAudioOutTest Load (0.0ms) [0m   [0;1mMysql::Error: Unknown column 'default_audio.phone_id' in 'where clause': SELECT * FROM `audio_out_tests` WHERE ((`default_audio`.`phone_id` IS NULL)) [0m

ActionView::TemplateError (Mysql::Error: Unknown column 'default_audio.phone_id' in 'where clause': SELECT * FROM `audio_out_tests` WHERE ((`default_audio`.`phone_id` IS NULL)) ) on line #7 of vendor/plugins/activescaffold/frontends/default/views/_form_attribute.html.erb:
4:     <label for="<%= active_scaffold_input_options(column, scope)[:id] %>"><%= column.label %></label>
5:   </dt>
6:   <dd>
7:     <%= active_scaffold_input_for column, scope %>
8:     <% if column.description -%>
9:       <span class="description"><%= column.description %></span>
10:     <% end -%>

    vendor/plugins/activescaffold/frontends/default/views/_form_attribute.html.erb:7
    vendor/plugins/activescaffold/frontends/default/views/_form.html.erb:14
    vendor/plugins/activescaffold/frontends/default/views/_form.html.erb:2
    vendor/plugins/activescaffold/frontends/default/views/_create_form.html.erb:38
    -e:2:in `load'
    -e:2

Rendered rescues/_trace (187.0ms)
Rendered rescues/_request_and_response (0.0ms)
Rendering rescues/layout (internal_server_error)
ActionView::TemplateError (You have a nil object when you didn't expect it!

Sergio Cambra .:: entreCables S.L. ::.

unread,
Aug 18, 2009, 10:24:26 AM8/18/09
to actives...@googlegroups.com
On Martes, 18 de Agosto de 2009 15:41:02 Allen Wyma escribió:
> Still not working for me. Gives me a HUGE error when I changed it to this
> AND tried to just create it a record. It would not even generate the create
> part.:
>
> module DefaultAudioHelper
> def options_for_association_conditions(association)
> if association.name == :speaker_test
> {'speaker_tests.phone_id' => @record.phone_id}
> elsif association.name == :audio_out_test_half
> {'audio_out_tests.phone_id' => @record.speaker_test.phone_id}
> elsif association.name == :audio_out_test_full
> {'audio_out_tests.phone_id' => @record.phone_id}
> else
> super
> end
> end
> end

It's right, when new form is shown, @record.speaker_test will be nil, so you
can't call phone_id. Change it to @record.speaker_test.try(:phone_id):
module DefaultAudioHelper
def options_for_association_conditions(association)
if association.name == :speaker_test
{'speaker_tests.phone_id' => @record.phone_id}
elsif association.name == :audio_out_test_half
{'audio_out_tests.phone_id' => @record.speaker_test.try(:phone_id)}
elsif association.name == :audio_out_test_full
{'audio_out_tests.phone_id' =>
@record.audio_out_test_half.try(:phone_id)}
>erb: 4: <label for="<%= active_scaffold_input_options(column,

Allen Wyma

unread,
Aug 18, 2009, 10:29:20 AM8/18/09
to actives...@googlegroups.com
Just tried:

module DefaultAudioHelper
  def options_for_association_conditions(association)
    if association.name == :speaker_test
      {:phone_id => @record.phone_id}
    elsif association.name == :audio_out_test_half
      {:phone_id => @record.speaker_test.try(:phone_id)}
    elsif association.name == :audio_out_test_full
      {:phone_id => @record.audio_out_test_half.try(:phone_id)}

    else
      super
    end
  end
end

Fails. Console:
scues/layout (internal_server_error)

  [4;35;1mSQL (0.0ms) [0m   [0mSET NAMES 'utf8' [0m
  [4;36;1mSQL (0.0ms) [0m   [0;1mSET SQL_AUTO_IS_NULL=0 [0m
  [4;35;1mDefaultAudio Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `default_audio` [0m
  [4;36;1mAudioOutTest Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `audio_out_tests` [0m
  [4;35;1mPhone Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `phones` [0m
  [4;36;1mFormFactor Columns (16.0ms) [0m   [0;1mSHOW FIELDS FROM `form_factors` [0m
  [4;35;1mManufacturer Columns (15.0ms) [0m   [0mSHOW FIELDS FROM `manufacturers` [0m
  [4;36;1mTechnology Columns (15.0ms) [0m   [0;1mSHOW FIELDS FROM `technologies` [0m
  [4;35;1mSpeakerTest Columns (0.0ms) [0m   [0mSHOW FIELDS FROM `speaker_tests` [0m


Processing DefaultAudioController#new (for 127.0.0.1 at 2009-08-18 09:28:28) [GET]
  Parameters: {"adapter"=>"_list_inline_adapter"}

  [4;36;1mUser Columns (0.0ms) [0m   [0;1mSHOW FIELDS FROM `users` [0m
  [4;35;1mUser Load (0.0ms) [0m   [0mSELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1 [0m
  [4;36;1mCACHE (0.0ms) [0m   [0;1mSELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1 [0m
  [4;35;1mAudioOutTest Load (0.0ms) [0m   [0mMysql::Error: Unknown column 'default_audio.phone_id' in 'where clause': SELECT * FROM `audio_out_tests` WHERE ((`default_audio`.`phone_id` IS NULL)) [0m


ActionView::TemplateError (Mysql::Error: Unknown column 'default_audio.phone_id' in 'where clause': SELECT * FROM `audio_out_tests` WHERE ((`default_audio`.`phone_id` IS NULL)) ) on line #7 of vendor/plugins/activescaffold/frontends/default/views/_form_attribute.html.erb:
4:     <label for="<%= active_scaffold_input_options(column, scope)[:id] %>"><%= column.label %></label>
5:   </dt>
6:   <dd>
7:     <%= active_scaffold_input_for column, scope %>
8:     <% if column.description -%>
9:       <span class="description"><%= column.description %></span>
10:     <% end -%>

    vendor/plugins/activescaffold/frontends/default/views/_form_attribute.html.erb:7

    vendor/plugins/activescaffold/frontends/default/views/_form.html.erb:14
    vendor/plugins/activescaffold/frontends/default/views/_form.html.erb:2
    vendor/plugins/activescaffold/frontends/default/views/_create_form.html.erb:38
    -e:2:in `load'
    -e:2

Rendered rescues/_trace (204.0ms)

Rendered rescues/_request_and_response (0.0ms)
Rendering rescues/layout (internal_server_error)

Allen Wyma

unread,
Aug 18, 2009, 10:40:11 AM8/18/09
to actives...@googlegroups.com
I wonder if I setup my Models correctly or not, cause I was working on another part and my ActiveRecord failed on me when I hand-made a DefaultAudio object. I'm doing a polymorphic association where There is a table of AudioOutTests, where they can either be half or full. I have a Boolean for if the record is half or not, and if it's not half, then it will be presumed to be full. I believe I setup my Models correctly, but can you check these for me? It could very well be the problem of why I keep getting errors for when I try to create a DefaultAudio:

class DefaultAudio < ActiveRecord::Base
  set_table_name "default_audio"
  has_one :audio_out_test_half, :as => :audio_out, :class_name => "AudioOutTest"
  has_one :audio_out_test_full, :as => :audio_out, :class_name => "AudioOutTest"
  belongs_to :speaker_test
  belongs_to :phone #relationship to Phone
 
  validates_uniqueness_of :phone_id
  validates_uniqueness_of :speaker_test_id, :scope => :phone_id
  validates_uniqueness_of :audio_out_test_half_id, :scope => :phone_id
  validates_uniqueness_of :audio_out_test_full_id, :scope => :phone_id
end

class AudioOutTest < ActiveRecord::Base
  include TestCommon
  belongs_to :phone
  belongs_to :audio_out, :polymorphic => true
  file_column :fr_png
  file_column :noise_png
  file_column :dynamics_png
  file_column :thd_png
  file_column :imd_png
  file_column :imdSwept_png
  file_column :cross_png
 
  validates_numericality_of :phone_id, :frPlus, :frMinus, :noise, :dRange, :thd, :imdNoise, :stereo
  validates_presence_of  :stereo_r, :imdNoise_r, :thd_r, :dRange_r, :noise_r, :fr_r, :frPlus, :frMinus, :noise, :dRange, :thd, :imdNoise, :stereo, :phone_id
 
  def name
    "Audio Out Test for #{self.phone.name}, Half rate:#{half} ,Dynamic Range: #{dRange} ,Distortion:#{thd}"
  end
end

Thanks for all of your help, so far. I hope I can get this thing working, soon!

Sergio Cambra .:: entreCables S.L. ::.

unread,
Aug 19, 2009, 3:16:02 AM8/19/09
to actives...@googlegroups.com
On Martes, 18 de Agosto de 2009 16:40:11 Allen Wyma escribió:
> I wonder if I setup my Models correctly or not, cause I was working on
> another part and my ActiveRecord failed on me when I hand-made a
> DefaultAudio object. I'm doing a polymorphic association where There is a
> table of AudioOutTests, where they can either be half or full. I have a
> Boolean for if the record is half or not, and if it's not half, then it
> will be presumed to be full. I believe I setup my Models correctly, but can
> you check these for me? It could very well be the problem of why I keep
> getting errors for when I try to create a DefaultAudio:

Try in script/console
assoc=DefaultAudio.reflect_on_association(:audio_out_test_half)
assoc.klass.find :all, :conditions => {:phone_id => 1}

I have tried to recreate your models and associations and it doesn't fails for
me.
> > vendor/plugins/activescaffold/frontends/default/views/_form_attribute.htm
> >l.erb: 4: <label for="<%= active_scaffold_input_options(column,
> > scope)[:id] %>"><%= column.label %></label>
> > 5: </dt>
> > 6: <dd>
> > 7: <%= active_scaffold_input_for column, scope %>
> > 8: <% if column.description -%>
> > 9: <span class="description"><%= column.description %></span>
> > 10: <% end -%>
> >
> >
> > vendor/plugins/activescaffold/frontends/default/views/_form_attribute.htm
> >l.erb:7
> > vendor/plugins/activescaffold/frontends/default/views/_form.html.erb:14
> > vendor/plugins/activescaffold/frontends/default/views/_form.html.erb:2
> >
> > vendor/plugins/activescaffold/frontends/default/views/_create_form.html.e

Allen Wyma

unread,
Aug 19, 2009, 8:40:18 AM8/19/09
to actives...@googlegroups.com
Changed around my models a bit different from before. Just added conditions to the associations in DefaultAudio where it makes it either be half or not. Here's the console dump fromm what you sent me:

>> assoc=DefaultAudio.reflect_on_association(:audio_out_test_half)
=> #<ActiveRecord::Reflection::AssociationReflection:0x3a290d8 @active_record=De
faultAudio(id: integer, speaker_test_id: integer, audio_out_test_full_id: intege
r, audio_out_test_half_id: integer, phone_id: integer, created_at: datetime, upd
ated_at: datetime), @name=:audio_out_test_half, @macro=:has_one, @options={:cond
itions=>["half = ?", true], :class_name=>"AudioOutTest", :as=>:audio_out}>

>> assoc.klass.find :all, :conditions => {:phone_id => 1}
=> [#<AudioOutTest id: 1, frPlus: #<BigDecimal:4f93c84,'0.1E1',4(8)>, frMinus: #
<BigDecimal:4f93c0c,'0.1E1',4(8)>, fr_r: "1", fr_png: "dsc00070.jpg", noise: #<B
igDecimal:4f93b6c,'0.1E1',4(8)>, noise_r: "1", noise_png: "dsc00070.jpg", dRange
: #<BigDecimal:4f93ae0,'0.1E1',4(8)>, dRange_r: "1", dynamics_png: "dsc00070.jpg
", thd: #<BigDecimal:4f93a54,'0.1E1',4(12)>, thd_r: "1", thd_png: "dsc00070.jpg"
, imdNoise: #<BigDecimal:4f939c8,'0.1E1',4(8)>, imdNoise_r: "1", imd_png: "dsc00
070.jpg", imdSwept_png: "dsc00070.jpg", stereo: #<BigDecimal:4f93914,'0.1E1',4(8
)>, stereo_r: "1", cross_png: "dsc00070.jpg", half: true, phone_id: 1, audio_out
_id: nil, audio_out_type: nil, created_at: "2009-08-17 19:09:36", updated_at: "2
009-08-17 19:09:36">, #<AudioOutTest id: 2, frPlus: #<BigDecimal:4f934f0,'0.0',4
(8)>, frMinus: #<BigDecimal:4f934b4,'0.0',4(8)>, fr_r: "0", fr_png: "dsc00070-th
umb.jpg", noise: #<BigDecimal:4f93428,'0.0',4(8)>, noise_r: "0", noise_png: "dsc
00070-thumb.jpg", dRange: #<BigDecimal:4f9339c,'0.0',4(8)>, dRange_r: "0", dynam
ics_png: "dsc00070-thumb.jpg", thd: #<BigDecimal:4f93310,'0.0',4(12)>, thd_r: "0
", thd_png: "dsc00070-thumb.jpg", imdNoise: #<BigDecimal:4f93284,'0.0',4(8)>, im
dNoise_r: "0", imd_png: "dsc00070-thumb.jpg", imdSwept_png: "dsc00070-thumb.jpg"
, stereo: #<BigDecimal:4f931d0,'0.0',4(8)>, stereo_r: "0", cross_png: "dsc00070-
thumb.jpg", half: false, phone_id: 1, audio_out_id: 2, audio_out_type: "DefaultA
udio", created_at: "2009-08-17 19:10:19", updated_at: "2009-08-17 19:10:19">, #<
AudioOutTest id: 3, frPlus: #<BigDecimal:4f92dac,'0.1E1',4(8)>, frMinus: #<BigDe
cimal:4f92d70,'0.1E1',4(8)>, fr_r: "1", fr_png: "cross.png", noise: #<BigDecimal
:4f92ce4,'0.1E1',4(8)>, noise_r: "1", noise_png: "cross.png", dRange: #<BigDecim
al:4f92c58,'0.1E1',4(8)>, dRange_r: "1", dynamics_png: "cross.png", thd: #<BigDe
cimal:4f92bcc,'0.1E1',4(12)>, thd_r: "1", thd_png: "cross.png", imdNoise: #<BigD
ecimal:4f92b40,'0.1E1',4(8)>, imdNoise_r: "1", imd_png: "cross.png", imdSwept_pn
g: "cross.png", stereo: #<BigDecimal:4f92a8c,'0.1E1',4(8)>, stereo_r: "1", cross
_png: "cross.png", half: true, phone_id: 1, audio_out_id: 1, audio_out_type: "De
faultAudio", created_at: "2009-08-18 16:48:42", updated_at: "2009-08-18 16:48:42
">]

Allen Wyma

unread,
Aug 19, 2009, 9:21:23 AM8/19/09
to actives...@googlegroups.com
Finally have this fixed. This seems to work so far with this in the DefaultAudioHelper:


module DefaultAudioHelper
  def options_for_association_conditions(association)
    if association.name == :speaker_test
      {'speaker_tests.phone_id' => @record.phone_id}
    elsif association.name == :audio_out_test_half
      {'audio_out_tests.phone_id' => @record.speaker_test.try(:phone_id)}
    elsif association.name == :audio_out_test_full
      {'audio_out_tests.phone_id' => @record.audio_out_test_half.try(:phone_id)}
    else
      super
    end
  end
end

And this in DefaultAudio Model:

class DefaultAudio < ActiveRecord::Base
  set_table_name "default_audio"
  has_one :audio_out_test_half, :as => :audio_out, :class_name => "AudioOutTest", :conditions => ['audio_out_tests.half = ?', true]
  has_one :audio_out_test_full, :as => :audio_out, :class_name => "AudioOutTest", :conditions => ['audio_out_tests.half = ?', false]

  belongs_to :speaker_test
  belongs_to :phone #relationship to Phone
 
  validates_uniqueness_of :phone_id
  validates_uniqueness_of :speaker_test_id, :scope => :phone_id
  validates_uniqueness_of :audio_out_test_half_id, :scope => :phone_id
  validates_uniqueness_of :audio_out_test_full_id, :scope => :phone_id
 
  def name
    "Default Audio for #{self.phone.name}"
  end
end
Reply all
Reply to author
Forward
0 new messages