Rails - relations

Discussion in 'Ruby' started by Israel Guerra, Jun 11, 2008.

  1. [Note: parts of this message were removed to make it a legal post.]

    Hi ppl.

    I am noob in rails and started an app to learn. I have the following
    scenario:

    table client with fileds: name, second_name
    table phone with fields: number, client_id
    table address with fields: street, number, client_id

    The migrations worked ok and the fields are created.

    Now i defined in the model that

    for client:
    has_many :phones
    has_many :adresses

    for phones and adresses:
    belongs_to :client

    I made a form to get some of these fields and add to the database:

    <% form_for:)clients) do |f| %>
    <p>
    <b>Name</b>
    <%= f.text_field :name %>
    </p>
    <p>
    <b>Second Name</b>
    <%= f.text_field :second_name%>
    </p>

    <p>
    <b>Number</b>
    <%= f.text_field :number%>
    </p>

    <p>
    <%= f.submit "Add" %>
    </p>
    <% end %>

    And in the controller i try to save it to the DB:

    @clients = Clients.new(params[:clients])
    @clients.save

    When i submit i get an error saying that number is not a method, it means it
    didnt associate number with a cliente. It's not "joining" the tables.

    Where did i made a mistake here?

    Thanks guys.


    --
    Guerra
     
    Israel Guerra, Jun 11, 2008
    #1
    1. Advertising

  2. Israel Guerra

    Damjan Rems Guest

    Israel Guerra wrote:
    > Hi ppl.
    >
    > I am noob in rails and started an app to learn. I have the following
    > scenario:
    >
    > table client with fileds: name, second_name
    > table phone with fields: number, client_id
    > table address with fields: street, number, client_id
    >
    > The migrations worked ok and the fields are created.
    >
    > Now i defined in the model that
    >
    > for client:
    > has_many :phones
    > has_many :adresses
    >
    > for phones and adresses:
    > belongs_to :client
    >
    > I made a form to get some of these fields and add to the database:
    >
    > <% form_for:)clients) do |f| %>
    > <p>
    > <b>Name</b>
    > <%= f.text_field :name %>
    > </p>
    > <p>
    > <b>Second Name</b>
    > <%= f.text_field :second_name%>
    > </p>
    >
    > <p>
    > <b>Number</b>
    > <%= f.text_field :number%>
    > </p>
    >
    > <p>
    > <%= f.submit "Add" %>
    > </p>
    > <% end %>
    >
    > And in the controller i try to save it to the DB:
    >
    > @clients = Clients.new(params[:clients])
    > @clients.save
    >
    > When i submit i get an error saying that number is not a method, it
    > means it
    > didnt associate number with a cliente. It's not "joining" the tables.
    >
    > Where did i made a mistake here?
    >
    > Thanks guys.



    1. Try asking this questions in rail snewsgroup
    2. You are trying to access field from other table. The right way to
    access the filed would be:
    client.phones[n].number

    and I don't know if this can be done with simple form.


    by
    TheR

    --
    Posted via http://www.ruby-forum.com/.
     
    Damjan Rems, Jun 11, 2008
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    Oh, it's true, i always end up mixing up rails list with ruby list!

    Thanks for the tips though :)

    On Wed, Jun 11, 2008 at 3:46 AM, Damjan Rems <> wrote:

    > Israel Guerra wrote:
    > > Hi ppl.
    > >
    > > I am noob in rails and started an app to learn. I have the following
    > > scenario:
    > >
    > > table client with fileds: name, second_name
    > > table phone with fields: number, client_id
    > > table address with fields: street, number, client_id
    > >
    > > The migrations worked ok and the fields are created.
    > >
    > > Now i defined in the model that
    > >
    > > for client:
    > > has_many :phones
    > > has_many :adresses
    > >
    > > for phones and adresses:
    > > belongs_to :client
    > >
    > > I made a form to get some of these fields and add to the database:
    > >
    > > <% form_for:)clients) do |f| %>
    > > <p>
    > > <b>Name</b>
    > > <%= f.text_field :name %>
    > > </p>
    > > <p>
    > > <b>Second Name</b>
    > > <%= f.text_field :second_name%>
    > > </p>
    > >
    > > <p>
    > > <b>Number</b>
    > > <%= f.text_field :number%>
    > > </p>
    > >
    > > <p>
    > > <%= f.submit "Add" %>
    > > </p>
    > > <% end %>
    > >
    > > And in the controller i try to save it to the DB:
    > >
    > > @clients = Clients.new(params[:clients])
    > > @clients.save
    > >
    > > When i submit i get an error saying that number is not a method, it
    > > means it
    > > didnt associate number with a cliente. It's not "joining" the tables.
    > >
    > > Where did i made a mistake here?
    > >
    > > Thanks guys.

    >
    >
    > 1. Try asking this questions in rail snewsgroup
    > 2. You are trying to access field from other table. The right way to
    > access the filed would be:
    > client.phones[n].number
    >
    > and I don't know if this can be done with simple form.
    >
    >
    > by
    > TheR
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    Guerra
     
    Israel Guerra, Jun 11, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nicolas STAMPF
    Replies:
    0
    Views:
    408
    Nicolas STAMPF
    May 4, 2004
  2. Joe Van Meer

    data relations and datasets inquiry

    Joe Van Meer, May 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    434
    Joe Van Meer
    May 5, 2004
  3. Joe Van Meer

    data relations

    Joe Van Meer, May 5, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    388
    Joe Van Meer
    May 5, 2004
  4. =?Utf-8?B?UmF2aQ==?=

    Dropdownlist - relations - asp.net

    =?Utf-8?B?UmF2aQ==?=, May 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,074
    avnrao
    May 17, 2004
  5. =?Utf-8?B?UmF2aQ==?=

    Dropdownlist - relations-asp.net-how to?

    =?Utf-8?B?UmF2aQ==?=, May 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    591
    Mary Chipman
    May 17, 2004
Loading...

Share This Page