One-to-Many Associations (newbie)

I have two databases Tours and Bands such that:

    • begin code - -

    class Band < ActiveRecord::Base
    belongs_to :tours
    end

    class Tour < ActiveRecord::Base
    has_one :bands
    end

    • end code - -

In my admin_tours I have added a bit of code that gives me a drop down
menu with a selection of all bands in Bands:

    • begin code - -

    Band
    <%= select("band", "band_id", Band.find_all.collect {|p| [ p.name,

p.id ] }) %>

    • end code - -

Of course you imagine that I am looking to have band_id save when i save
tours. I am having a difficult time figuring out what code works I
should put in my ‘admin_tours_controller’. As I have scaffolded this out
so I am assuming I should be adding some sort of save code to ‘update’
and ‘create’.

I am willing to pay someone to walk me thru this (aim? tele?) or if you
have any quick (free) advice that would work too.

: )

thanks!


Mason K.

http://www.poccuo.com

    • begin code - -

    class Band < ActiveRecord::Base
    belongs_to :tours
    end

    class Tour < ActiveRecord::Base
    has_one :bands
    end

First, this should be has_one :band, not :bands

    • begin code - -

    Band
    <%= select("band", "band_id", Band.find_all.collect {|p| [ p.name,

p.id ] }) %>

I don’t know how did you built your DB schema, but it should be
something like this


table bands:
id
name
tour_id
(other columns)

table tours:
id
location
(other columns)


in this case, when using belongs_to - has_one, the child element (aka
the one who belongs to other) need to have a column indicating it’s
association id (in my ex. it’s tour_id) that’s how you will get your
relation.

as for the select it’s should be <%= select_tag “tour”, “band_id”,
Band.find(:all).collect {|e| [e.name, e.id]} %>

if i missed your point, i’ll be happy to help you for free of course if
you refer to my by mail ([email protected]) or by MSN
([email protected]).

hope this helps