Bick
Bick

Reputation: 18521

ruby on rails - after establishing many to many connection - how do I display data

I have established many to many relation with the help of this site.
I have added has_and_belongs_to_many in the model
Now I want to display data from that object on the screen
I assume I should do it in the html.erb
lets say that I want it on the index form and in the edit/new form

how can I present that? thanks.

EDIT:
I have two objects :
i.e. person and states - A person can have many states)
I want to have a screen where I could present all the states of the person and another screen where I can add him another states.
as I mentioned I created rb scripts like this (for both)

class CreateStates < ActiveRecord::Migration
def change
  create_table :states do |t|
    t.string :name
    t.timestamps
  end
 end
end

and a join table

def up
  create_table 'persons_states', :id => false do |t|
  t.integer :persons_id
  t.integer :states_id
end

end and in the model added has_and_belongs_to_many :ingredients

How do I update my code of ther gui? thank

Upvotes: 0

Views: 285

Answers (3)

David Underwood
David Underwood

Reputation: 4966

Kulgar's answer deals with setting up the relations, which is correct. With those in place you can display the info as follows in your view:

<%= @person.name %>
<ul>
  <% @person.states.each do |state| %>
    <li><%= state.name %></li>
  <% end %>
</ul>

This assumes you have an appropriate controller action which sets @person to an instance of the Person class. Maybe something like this:

def show
  @person = Person.find(params[:id])
end

You could go further and add a partial view for displaying the State class and just reference it in your view:

<%= @person.name %>
<% @person.states.each do |state| %>
  <%= render state %>
<% end %>

This will use _state.html.erb to render the partial and will pass the local variable state into it.

The important part in all this is that @person.states will refer to the states associated with the person :)

Upvotes: 1

Jonathan
Jonathan

Reputation: 11494

If you have a Person that can have many states it is as simple as:

Models person.rb

has_many :states

state.rb

belongs_to :person

The migrations you generate:

rails generate model State person_id:integer name:string
rails generate model Person name:string

If you need to add this person_id foreign key to the person just do:

rails generate migration Add_Person_ID_To_States person_id:integer
rake db:migrate

There's usually no need for has_and_belongs_to_many.

Upvotes: 1

Kulgar
Kulgar

Reputation: 1865

you should take a look at this guide: Associations - Has and belong to many official guide

Without the name of your models/resources bound with this association, I just can't help you further. Please provide them if you want a more accurate answer.

Ok, answered before you edited your question.

So you want a "has_and_belong_to_many" association between your Person and States model. So in your "Person" model you shoud have: has_and_belongs_to_many :states. And in your "State" model you should have: has_and_belongs_to_many :people (plural of person). Your join table with a "has_and_belongs_to_many" association HAS to be "people_states", you don't really have the choice as this is a convention.

You could also check the official documentation it provides helpful information.

Upvotes: 1

Related Questions