Hey! I'm decent at Ruby, but pretty darn new to Rails. Working through
some tutorial stuff, and don't fully understand what direction to take. I
want to create a one-to-many association; how do I accomplish this? Do I
create the association with MySQL (the DB I'm using) first, and then just
have Rails take advantage of it? Do I use "migration" (which I only
kinda-sorta "get")? Or is there some other mechanism entirely?

Apologies for how dumb this probably sounds; trying to get the full feel of
Rails, and there are definitely some places I'm still not quite
understanding the concepts.

Thanks,

-Ken

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/22e7d572-4f99-459e-a26c-3b82e441daa5%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Hassan Schroeder at Feb 10, 2014 at 5:15 pm

    On Mon, Feb 10, 2014 at 8:50 AM, Ken D'Ambrosio wrote:
    I want to create a one-to-many association; how do I accomplish this?
    Have you read these?

       http://guides.rubyonrails.org/association_basics.html
       http://guides.rubyonrails.org/migrations.html

    In general you should make changes to your DB through migrations
    only, so you can reverse, repeat, insure identical changes are made
    to other (deployment) systems, etc.

    --
    Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
    http://about.me/hassanschroeder
    twitter: @hassan

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yAayvNvactqEQ9VOPQrktOxh%3DsLsJuRNd%2BG40%2B05P86MA%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Colin Law at Feb 10, 2014 at 8:28 pm

    On 10 February 2014 16:50, Ken D'Ambrosio wrote:
    Hey! I'm decent at Ruby, but pretty darn new to Rails. Working through
    some tutorial stuff, and don't fully understand what direction to take. I
    want to create a one-to-many association; how do I accomplish this?
    Work right through a good tutorial such as railstutorial.org (free to
    use online) and all the basics (including associations) will become
    clear.

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvN0AVV6G%2BJH_3HY6mKXtT5tgbj2eb%3D%3DMaQfGxG_rM5Lw%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • James Turley at Feb 11, 2014 at 11:11 am
    Example: an owner has many cats.

    In your Cat model, add the line:
    belongs_to :owner

    in your Owner model, add the line:
    has_many :cats

    You then need to run a migration on your cats table, to add the foreign key
    for owners, so Rails can build the associations. From the command line:

    rails g migration add_owner_id_to_cats owner_id:integer

    This should create a migration file (in the /db folder), with something
    like the following in it:

    class AddFooToPosts < ActiveRecord::Migration
       def change
         add_column :cats, :owner_id, :integer # ie, table, field name, field
    type
       end
    end

    then, run your migration.

    rake db:migrate

    Thanks to the convention over configuration approach, rails will
    automagically work out that owner_id is a foreign key to the owners table.
    from there, you'll be able to access cats through owners, and owners
    through cats:

    mog.owner # "Steve"
    steve.cats.first # "Mog"

    That, of course, is just the tip of the iceberg - you can define all kinds
    of odd relations, and customise them as much as you want.

    JT



    On Mon, Feb 10, 2014 at 8:28 PM, Colin Law wrote:
    On 10 February 2014 16:50, Ken D'Ambrosio wrote:
    Hey! I'm decent at Ruby, but pretty darn new to Rails. Working through
    some tutorial stuff, and don't fully understand what direction to take. I
    want to create a one-to-many association; how do I accomplish this?
    Work right through a good tutorial such as railstutorial.org (free to
    use online) and all the basics (including associations) will become
    clear.

    Colin

    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit
    https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvN0AVV6G%2BJH_3HY6mKXtT5tgbj2eb%3D%3DMaQfGxG_rM5Lw%40mail.gmail.com
    .
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAAb4X%3Dy9O7OFv4pzBQE_7MqAD9KNYSZQ53ZZUXbEz06V3G0S-g%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • James Turley at Feb 11, 2014 at 11:12 am
    NB: AddFooToPosts would be AddOwnerIdToCats, I just messed up the copy
    paste.

    On Tue, Feb 11, 2014 at 11:10 AM, James Turley wrote:

    Example: an owner has many cats.

    In your Cat model, add the line:
    belongs_to :owner

    in your Owner model, add the line:
    has_many :cats

    You then need to run a migration on your cats table, to add the foreign
    key for owners, so Rails can build the associations. From the command line:

    rails g migration add_owner_id_to_cats owner_id:integer

    This should create a migration file (in the /db folder), with something
    like the following in it:

    class AddFooToPosts < ActiveRecord::Migration
    def change
    add_column :cats, :owner_id, :integer # ie, table, field name, field
    type
    end
    end

    then, run your migration.

    rake db:migrate

    Thanks to the convention over configuration approach, rails will
    automagically work out that owner_id is a foreign key to the owners table.
    from there, you'll be able to access cats through owners, and owners
    through cats:

    mog.owner # "Steve"
    steve.cats.first # "Mog"

    That, of course, is just the tip of the iceberg - you can define all kinds
    of odd relations, and customise them as much as you want.

    JT



    On Mon, Feb 10, 2014 at 8:28 PM, Colin Law wrote:

    On 10 February 2014 16:50, Ken D'Ambrosio <flyingtoasters1024@gmail.com>
    wrote:
    Hey! I'm decent at Ruby, but pretty darn new to Rails. Working through
    some tutorial stuff, and don't fully understand what direction to take. I
    want to create a one-to-many association; how do I accomplish this?
    Work right through a good tutorial such as railstutorial.org (free to
    use online) and all the basics (including associations) will become
    clear.

    Colin

    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit
    https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvN0AVV6G%2BJH_3HY6mKXtT5tgbj2eb%3D%3DMaQfGxG_rM5Lw%40mail.gmail.com
    .
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAAb4X%3Dwut_fWAB_7B40NB8_dmJ7sXJNgEgCpg%2B-obG8NbqQ36A%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 10, '14 at 4:55p
activeFeb 11, '14 at 11:12a
posts5
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase