0

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

Bick
  • 17,833
  • 52
  • 146
  • 251
  • I think you need to show more about how the relation has been set up. Otherwise you may just want to use `has_many` and `belongs_to` instead. How can we know not to suggest this from your question? – Jonathan Apr 27 '12 at 16:30
  • I can't understand what the purpose of `states` is for. It's so ambiguous. Even though you added data to the question it is still ambiguous. I can't see how `origin_id` or `ingredient_id` relate to your `states` table. In rails, you set up the relation in the model. Someone can provide you an easy answer to read the associations guide, good for him and maybe he gets an upvote (not from me) but your question needs more effort - and go and read the guide anyway. – Jonathan Apr 27 '12 at 17:21

3 Answers3

1

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.

Kulgar
  • 1,855
  • 19
  • 26
  • I'm not sure how Rails work with the plural of "person" in its convention. I hope it does know that the plural of person is people. When dealing with models and convention I'm not sure it does know that.. – Kulgar Apr 27 '12 at 16:49
  • I'm not sure he really wants to have a join table in this scenario but one up vote for editing your solution to explain how he should use it – Jonathan Apr 27 '12 at 17:27
1

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.

Jonathan
  • 10,936
  • 8
  • 64
  • 79
  • 1
    Hey, your right, I should have think about this solution! I'll up vote your answer too. ;) – Kulgar Apr 27 '12 at 18:36
  • 1
    Unless states can be shared between people. For example, if states are US states and he's trying to build a history of states people have visited, then the has_and_belongs_to_many relationship makes sense. – David Underwood Apr 28 '12 at 20:15
  • I like that example thanks. I guess it's up to him what the interpretation is – Jonathan Apr 28 '12 at 20:22
1

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 :)

David Underwood
  • 4,908
  • 1
  • 19
  • 25