Kenny Ortmann was kind enough to put this up for me last night.

http://github.com/yairgo/dynamic_selects/tree/master

I think the problem I was having was in understanding why I need the third 
table. If I have a one-to-many relationship, isn't a foreign key in the child 
table sufficient to describe the relationship?  If it were HABTM, I would need 
a join table like authors_books.  Why do I need the users table to store keys 
in this example?

Sorry for asking such a noob question.  One observation that I have on AS is 
that it forces you to really think through the associations.

Cheers! Paul


--- On Fri, 8/21/09, Sergio Cambra .:: entreCables S.L. ::. 
<[email protected]> wrote:

> From: Sergio Cambra .:: entreCables S.L. ::. <[email protected]>
> Subject: Re: Yet another question on dynamic selects
> To: [email protected]
> Date: Friday, August 21, 2009, 12:48 AM
> 
> On Jueves, 20 de Agosto de 2009 18:39:07 Paul Hamann
> escribió:
> > I just want "Hello world" for a chained fields
> example.
> >
> > On the old Active Scaffold site, there was a 'demos'
> section.  That section
> > included code for common yet complicated scenarios. I
> would like something
> > similar for chaining fields.  Many others have
> sought the same thing from
> > what I've seen while Googling.
> >
> > And FWIW, here's some philosophy.  I've seen many
> software companies and
> > projects fail for misunderstanding one fact.  80%
> of potential developers
> > only have 20% of the skills.  That 80% learns
> through copy/paste of working
> > examples. Focus on providing better samples to work
> with, and you will
> > increase your addressable market by 400%.
> 
> Here is an example, I haven't tried it, but I think it will
> work:
> 
> script/generate resource author name:string
> script/generate resource book author:references
> name:string
> script/generate resource user name:string
> favourite_author:references favourite_book:references
> 
> # app/models/book.rb:
> class Book < ActiveRecord::Base
>   belongs_to :author
>   has_many :users, :foreign_key =>
> 'favourite_book_id'
> end
> 
> # app/models/author.rb:
> class Author < ActiveRecord::Base
>   has_many :books
>   has_many :users, :foreign_key =>
> 'favourite_author_id'
> end
> 
> # app/models/user.rb:
> class User < ActiveRecord::Base
>   belongs_to :favourite_author, :class => 'Author'
>   belongs_to :favourite_book, :class => 'Book'
> end
> 
> # app/controllers/authors_controller.rb
> class AuthorsController < ApplicationController
>   active_scaffold
> end
> 
> # app/controllers/books_controller.rb
> class BooksController < ApplicationController
>   active_scaffold
> end
> 
> # app/controllers/users_controller.rb
> class UsersController < ApplicationController
>   active_scaffold do |config|
>     config.columns[:favourite_author].form_ui =
> :select
>     config.columns[:favourite_author].options =
> {:update_column => :favourite_book}
>     config.columns[:favourite_book].form_ui =
> :select
>   end
> end
> 
> # app/helpers/users_helper.rb
> class UsersHelper
>   def options_for_association_conditions(association)
>     if association.name == :favourite_book
>       {'books.author_id' =>
> @record.author_id}
>     else
>       super
>     end
>   end
> end
> 
> >
> > Thanks! Paul
> >
> > --- On Thu, 8/20/09, Sergio Cambra .:: entreCables
> S.L. ::. <[email protected]>
> wrote:
> > > From: Sergio Cambra .:: entreCables S.L. ::.
> <[email protected]>
> > > Subject: Re: Yet another question on dynamic
> selects
> > > To: [email protected]
> > > Date: Thursday, August 20, 2009, 9:11 AM
> > >
> > > On Jueves, 20 de Agosto de 2009 17:59:16 Paul
> Hamann
> > >
> > > escribió:
> > > > I ended up with a rather contrived example
> after
> > >
> > > looking at too many code
> > >
> > > > samples.  I really just want "Hello
> world."
> > >
> > > You are trying something harder thant Hello
> world. A hello
> > > world example
> > > shouldn't use chained fields in my opinion.
> > >
> > > > For survey, there are n number of surveys. 
> > >
> > > Surveys have n number of survey
> > >
> > > > questions. When a specific survey is
> selected, the
> > >
> > > questions for that
> > >
> > > > survey show up as options in the second
> select. 
> > >
> > > Like I said, it's a bit
> > >
> > > > contrived.
> > >
> > > But you are setting that in survey create and
> update forms.
> > > Scenary you are
> > > describing is easier, and more logical IMO, for a
> model
> > > with belongs_to
> > >
> > > :survey and belongs_to :survey_question
> > > :
> > > > Thanks for the help, Paul
> > > >
> > > > --- On Thu, 8/20/09, Sergio Cambra .::
> entreCables
> > >
> > > S.L. ::.
> > > <[email protected]>
> > >
> > > wrote:
> > > > > From: Sergio Cambra .:: entreCables
> S.L. ::.
> > >
> > > <[email protected]>
> > >
> > > > > Subject: Re: Yet another question on
> dynamic
> > >
> > > selects
> > >
> > > > > To: [email protected]
> > > > > Date: Thursday, August 20, 2009, 12:49
> AM
> > > > >
> > > > > On Jueves, 20 de Agosto de 2009
> 03:48:04 Paul
> > >
> > > Hamann
> > >
> > > > > escribió:
> > > > > > Thanks again for all your help.
> Sadly, I'm
> > >
> > > still
> > >
> > > > > missing something. [BTW,
> > > > >
> > > > > > there's a search_sql in the thread
> example,
> > >
> > > but not in
> > >
> > > > > the wiki example?]
> > > > >
> > > > > > Here's the code:
> > > > > >
> > > > > > ============ schema
> > >
> > > ===============================
> > >
> > > > > >  create_table
> "survey_questions",
> > > :
> > > :force =>
> > > :
> > > > > true do |t|
> > > > >
> > > > > >     t.integer
> > > > >
> > > > > "survey_id", 
>    :limit =>
> > >
> > > 11
> > >
> > > > > >     t.text
> > > > >
> > > > >    "question_text"
> > > > >
> > > > > > 
>    t.datetime
> > >
> > > "created_at"
> > >
> > > > > > 
>    t.datetime
> > >
> > > "updated_at"
> > >
> > > > > >   end
> > > > > >
> > > > > >   create_table
> "surveys",
> > > :
> > > :force =>
> > > :
> > > > > true do |t|
> > > > >
> > > > >
> > > > >
> > > > >   
> t.string   "name"
> > > > >
> > > > > > 
>    t.datetime
> > >
> > > "created_at"
> > >
> > > > > > 
>    t.datetime
> > >
> > > "updated_at"
> > >
> > > > > >   end
> > > > > >
> > > > > > ============ Model: survey.rb
> =========
> > > > > >
> > > > > > class Survey <
> ActiveRecord::Base
> > > > > >
> > > > > >   has_many
> :survey_questions
> > > > > >
> > > > > >   def to_tabel
> > > > > >    "#{name}"
> > > > > >   end
> > > > > > end
> > > > > >
> > > > > > ============Model:
> survey_question.rb ====
> > > > > >
> > > > > > class SurveyQuestion <
> > >
> > > ActiveRecord::Base
> > >
> > > > > >   belongs_to
> :survey
> > > > > >
> > > > > >   def to_label
> > > > > >    "#{question_text}"
> > > > > >   end
> > > > > > end
> > > > > >
> > > > > > =================
> survey_helper.rb
> > >
> > > ==================
> > >
> > > > > > module SurveyHelper
> > > > > >
> > > > > >    def
> > > > >
> > > > >
> options_for_association_conditions(association)
> > > > >
> > > > > >      if
> association.name ==
> > > > > >
> > > > > :survey_question
> > > > > :
> > > > > >   
> > > > >
> > > > >   
> {'survey_questions.survey_id' =>
> > > > > @record.survey_id}
> > > > >
> > > > > >      else
> > > > > >   
>    super
> > > > > >      end
> > > > > >    end
> > > > > >
> > > > > > end
> > > > > >
> > > > > > =============
> survey_controller.rb
> > > > >
> > > > > =====================
> > > > >
> > > > > > class SurveyController <
> > >
> > > ApplicationController
> > >
> > > > > >  layout "default"
> > > > > >  active_scaffold :survey
> > > > > >
> > > > > >  active_scaffold do |config|
> > > > > > 
> > >
> > >    config.columns[:name].form_ui
> > >
> > > > > = :select
> > > > >
> > > > > > 
> > >
> > >    config.columns[:name].options
> > >
> > > > > = { :update_column =>
> ':survey_question'
> > > > >
> > > > > > }
> config.columns['question_text'].form_ui =
> > > :
> > > :select
> > > :
> > > > > >   end
> > > > > > end
> > > > >
> > > > > form_ui select automatically fills the
> options
> > >
> > > when column
> > >
> > > > > is an association
> > > > > (neither name nor question_text are
> > >
> > > associations). You
> > >
> > > > > can't set question_text
> > > > > form_ui from SurveyController because
> that column
> > >
> > > belongs
> > >
> > > > > to SurveyQuestion
> > > > > model, you should do it in
> > >
> > > SurveyQuestionsController.
> > >
> > > > > I don't understand what you want to
> get. What
> > >
> > > options
> > >
> > > > > should be shown for
> > > > > name?
> > >
> > >
> ==========================================================
> > >
> > > > > > --- On Wed, 8/19/09, Kerry Foley
> <[email protected]>
> > > > >
> > > > > wrote:
> > > > > > > From: Kerry Foley <[email protected]>
> > > > > > > Subject: Re: Yet another
> question on
> > >
> > > dynamic
> > >
> > > > > selects
> > > > >
> > > > > > > To: [email protected]
> > > > > > > Date: Wednesday, August 19,
> 2009, 5:32
> > >
> > > PM
> > >
> > > > > > > It sounds like your column
> name is
> > >
> > > incorrect. You
> > >
> > > > > didn't
> > > > >
> > > > > > > paste you're
> > > > > > > code so I can't say for sure.
> You
> > >
> > > should be doing
> > >
> > > > > something
> > > > >
> > > > > > > like
> > > > > > >
> config.columns[:project_type].form_ui =
> > > :
> > > :select
> > > :
> > > > > > > #belongs_to
> > > > > > > or
> > > > > > >
> config.columns[:regions].form_ui =
> > > > > > >
> > > > > > >
> :select   #has_many
> > > > > > >
> > > > > > > it shouldn't be "_id" - it's
> the
> > >
> > > association
> > >
> > > > > name, not the
> > > > >
> > > > > > > column name.
> > > > > > >
> > > > > > > There are examples on the
> wiki e.g
> > >
> > > Getting
> > >
> > > > > Started;
> > > > >
> > > > > > > http://wiki.github.com/activescaffold/active_scaffold
> > > > > > >
> > > > > > > You don't need to build the
> select.
> > > > > > >
> > > > > > > I think there's a more or
> less complete
> > >
> > > set of
> > >
> > > > > controller,
> > > > >
> > > > > > > model and
> > > > > > > helper code for what you are
> trying to
> > >
> > > do in the
> > >
> > > > > thread
> > > > >
> > > > > > > previously sent.
> > > > > > >
> > > > > > > http://groups.google.com/group/activescaffold/browse_thread/threa
> > > > > > >d/2a e130 edb1d0e2d2
> > > > > > >
> > > > > > >
> > > > > > > Regards,
> > > > > > > Kerry
> > > > > > >
> > > > > > > Me wrote:
> > > > > > > > Bare with me here. 
> I'm a
> > >
> > > little slow.
> > >
> > > > > So I tried
> > > > >
> > > > > > > the code, and I get:
> > > > > > > > You have a nil object
> when you
> > >
> > > didn't expect
> > >
> > > > > it!
> > > > >
> > > > > > > > The error occurred
> while
> > >
> > > evaluating
> > >
> > > > > nil.form_ui=
> > > > >
> > > > > > > > Is there enough smarts
> built-in to
> > >
> > > populate
> > >
> > > > > the select
> > > > >
> > > > > > > boxes? Or do I
> > > > > > >
> > > > > > > > need something like:
> > > > > > > >
> > > > > > > > a=
> Author.find(:all).collect {
> > > |
> > > |a|
> > > |
> > > > > a.name  }
> > > > >
> > > > > > > > If that kind of code is
> required,
> > >
> > > do I put
> > >
> > > > > that in the
> > > > >
> > > > > > > helper form
> > > > > > >
> > > > > > > > override?  Any
> chance I can
> > >
> > > see one
> > >
> > > > > complete
> > > > >
> > > > > > > example that works? 
> I've
> > > > > > >
> > > > > > > > been banging my head on
> this for
> > >
> > > a
> > >
> > > > > while.  When I
> > > > >
> > > > > > > Google, I just come
> > > > > > >
> > > > > > > > across more people that
> have been
> > >
> > > banging
> > >
> > > > > there heads
> > > > >
> > > > > > > on this.
> > > > > > >
> > > > > > > > Thanks! Paul
> > > > > > > >
> > > > > > > > On Aug 19, 1:58 pm,
> Kerry Foley
> > >
> > > <[email protected]>
> > >
> > > > > > > wrote:
> > > > > > > >   
> > > > > > > >
> > > > > > > >> http://groups.google.com/group/activescaffold/browse_thread/th
> > > > > > > >>read /2a. ..
> > > > > > > >>
> > > > > > > >> Regards,
> > > > > > > >> Kerry
> > > > > > > >>
> > > > > > > >> Me wrote:
> > > > > > > >> 
>    
> > > > > > > >>
> > > > > > > >>> Greetings! 
> First
> > >
> > > post.
> > >
> > > > > > >
> >>>   
> > >
> > >    
> > >
> > > > > > > >>> I've been
> greping the web
> > >
> > > for an AS
> > >
> > > > > example of
> > > > >
> > > > > > > populating a parent
> > > > > > >
> > > > > > > >>> select box, and
> then
> > >
> > > dynamically
> > >
> > > > > changing a
> > > > >
> > > > > > > child select box through
> > > > > > >
> > > > > > > >>> an "onchange"
> event. 
> > >
> > > I've seen
> > >
> > > > > the
> > > > >
> > > > > > > observer approach, but not
> with
> > > > > > >
> > > > > > > >>> AS.  Is
> there support
> > >
> > > for such
> > >
> > > > > a thing?
> > > > >
> > > > > > >
> >>>   
> > >
> > >    
> > >
> > > > > > > >>> I found this
> thread where
> > >
> > > this same
> > >
> > > > > question
> > > > >
> > > > > > > was asked.
> > > > > > >
> > > > > > >
> >>>   
> > >
> > >    
> > >
> > > > > > > >>> http://www.mail-archive.com/[email protected]/m
> > > > > > > >>>sg00 108. ...
> > > > > > > >>> The poor guy got
> a rather
> > >
> > > prickly
> > >
> > > > > > > response.  He eventually
> posted
> > >
> > > his
> > >
> > > > > > > >>> solution in the
> last msg
> > >
> > > of the
> > >
> > > > > thread.
> > > > >
> > > > > > > Unfortunately, I can't get
> > > > > > >
> > > > > > > >>> that solution
> to
> > >
> > > work.  The
> > >
> > > > > example [in
> > > > >
> > > > > > > plain English] was, "Surveys
> > > > > > >
> > > > > > > >>> have many
> survey_questions
> > >
> > > and
> > >
> > > > > surveys
> > > > >
> > > > > > > have_many
> survey_options."  If
> > > > > > >
> > > > > > > >>> options are
> meant to be
> > >
> > > answers to
> > >
> > > > > questions,
> > > > >
> > > > > > > then it should be
> > > > > > >
> > > > > > > >>> "Surveys have
> many
> > >
> > > questions.
> > >
> > > > > Questions
> > > > >
> > > > > > > have many options [multiple
> > >
> > > choice]."   That
> > >
> > > > > would be three
> > > > >
> > > > > > > levels.  I tried looking
> at the
> > > > > > >
> > > > > > > >>> survey_options
> table as
> > >
> > > some type of
> > >
> > > > > join with
> > > > >
> > > > > > > both foreign keys
> > > > > > >
> > > > > > > >>> [survey_id,
> > > > >
> > > > > survey_question_id].  If that
> > > > >
> > > > > > > were the case, then survey
> > > > > > >
> > > > > > > >>> "has many
> questions
> > >
> > > through
> > >
> > > > > > > survey_options."  That
> wasn't
> > >
> > > listed in
> > >
> > > > > > > >>> the example. 
> > >
> > > Clearly, I'm
> > >
> > > > > confused
> > > > >
> > > > > > > somewhere.
> > > > > > >
> > > > > > >
> >>>   
> > >
> > >    
> > >
> > > > > > > >>> I would
> appreciate it if
> > >
> > > someone
> > >
> > > > > could explain
> > > > >
> > > > > > > the example referenced
> > > > > > >
> > > > > > > >>> in the last
> message of the
> > >
> > > thread
> > >
> > > > > linked to
> > > > >
> > > > > > > above.  Alternatively.
> I
> > > > > > >
> > > > > > > >>> would appreciate
> a clear
> > >
> > > example of
> > >
> > > > > this
> > > > >
> > > > > > > pattern.  I think that
> this
> > > > > > >
> > > > > > > >>> is a common
> situation
> > >
> > > where some
> > >
> > > > > documentation
> > > > >
> > > > > > > would benefit many
> > > > > > >
> > > > > > > >>> people.
> > > > > > >
> >>>   
> > >
> > >    
> > >
> > > > > > > >>> Thanks! Paul
> > > > > > >
> >>>   
> > >
> > >    
> > >
> > > > > > > >   
> > > > >
> > > > > --
> > > > > Sergio Cambra .:: entreCables S.L. ::.
> > > > > Mariana Pineda 23, 50.018 Zaragoza
> > > > > T) 902 021 404 F) 976 52 98 07 E) [email protected]
> > >
> > > --
> > > Sergio Cambra .:: entreCables S.L. ::.
> > > Mariana Pineda 23, 50.018 Zaragoza
> > > T) 902 021 404 F) 976 52 98 07 E) [email protected]
> >
> > 
> -- 
> Sergio Cambra .:: entreCables S.L. ::.
> Mariana Pineda 23, 50.018 Zaragoza
> T) 902 021 404 F) 976 52 98 07 E) [email protected]
> 
> 
> > 
> 

--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/activescaffold?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to