PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Show info from table wich is joined
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Show info from table wich is joined

Réponse
 
LinkBack Outils de la discussion
Vieux 06/11/2007, 14h53   #1
Remco Swoany
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Show info from table wich is joined

Hi,

<% @user.email %>
...is working

<% @user.educations do |education| %>
<li><%= education.name%></li>
...is NOT working.

This is because there is a join-table involved..below the mirgation

class AddTableEducationsUsers < ActiveRecord::Migration
def self.up
create_table :educations_users do |t|
t.column :education_id, :integer
t.column :user_id, :integer

end
add_index "educations_users", "education_id"
add_index "educations_users", "user_id"

end
def self.down
drop_table :educations_users
end
end

I want on the user-page the occupations of the user...any suggestions??

remco
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 07/11/2007, 07h38   #2
Jacob Basham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Show info from table wich is joined

Did you setup the AR models?

User
has_many :education

Education
belongs_to :user

On Nov 6, 2007, at 8:53 AM, Remco Swoany wrote:

> Hi,
>
> <% @user.email %>
> ...is working
>
> <% @user.educations do |education| %>
> <li><%= education.name%></li>
> ...is NOT working.
>
> This is because there is a join-table involved..below the mirgation
>
> class AddTableEducationsUsers < ActiveRecord::Migration
> def self.up
> create_table :educations_users do |t|
> t.column :education_id, :integer
> t.column :user_id, :integer
>
> end
> add_index "educations_users", "education_id"
> add_index "educations_users", "user_id"
>
> end
> def self.down
> drop_table :educations_users
> end
> end
>
> I want on the user-page the occupations of the user...any
> suggestions??
>
> remco
> --
> Posted via http://www.ruby-forum.com/.
>



  Réponse avec citation
Vieux 07/11/2007, 18h22   #3
Remco Swoany
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Show info from table wich is joined

Jacob Basham wrote:
> Did you setup the AR models?
>
> User
> has_many :education
>
> Education
> belongs_to :user


Hi Jacob,

My models are..

User
has_and_belongs_to_many :educations

Education
has_and_belongs_to_many :users


Because a user can have many educations and eductions can have many
users.

remco
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 07/11/2007, 18h30   #4
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Show info from table wich is joined

On Nov 7, 2007 12:22 PM, Remco Swoany <remco.zwaan@gmail.com> wrote:
> Jacob Basham wrote:
> > Did you setup the AR models?
> >
> > User
> > has_many :education
> >
> > Education
> > belongs_to :user

>
> Hi Jacob,
>
> My models are..
>
> User
> has_and_belongs_to_many :educations
>
> Education
> has_and_belongs_to_many :users
>
>
> Because a user can have many educations and eductions can have many
> users.


Are you trying to move away from the DB relational model (i.e. you
plan to alter the DB), or just trying to get Ruby to work with it?

By the sound of it, you have something in the DB that looks like
(forgive the crude notation)...

Users <-- Expertise --> Educations

3 tables?

Todd

  Réponse avec citation
Vieux 07/11/2007, 19h18   #5
Remco Swoany
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Show info from table wich is joined

Todd Benson wrote:
> On Nov 7, 2007 12:22 PM, Remco Swoany <remco.zwaan@gmail.com> wrote:
>>
>> users.

> Are you trying to move away from the DB relational model (i.e. you
> plan to alter the DB), or just trying to get Ruby to work with it?
>
> By the sound of it, you have something in the DB that looks like
> (forgive the crude notation)...
>
> Users <-- Expertise --> Educations
>
> 3 tables?
>
> Todd


I have 3 tables

users
educations
educactions_users

The educactions_users is my join table, with the columns users_id and
educations_id.

A part of the user-edit-view looks like this, where the user can
specified there educations. This works..after getting grey hair!!

<h4>educations</h4>
<% for education in Education.find(:all) %>
<div>
<%= check_box_tag "user[education_ids][]", education.id,
@user.educations.include?(education) %>
<%= education.name%>
</div>
<% end %>

But.....

In the show-view i want a list of educations wich the user has selected.
I thought this.

<h4>Educations</h4>
<ul>
<% @user.educations do |education| %>
<li><%= education.name%></li>
<% end %>
</ul>

This get empty value..

This is my problem..

Grtz..remco
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 07/11/2007, 23h32   #6
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Show info from table wich is joined

On Nov 7, 2007 1:18 PM, Remco Swoany <remco.zwaan@gmail.com> wrote:
> Todd Benson wrote:
> > On Nov 7, 2007 12:22 PM, Remco Swoany <remco.zwaan@gmail.com> wrote:
> >>
> >> users.

> > Are you trying to move away from the DB relational model (i.e. you
> > plan to alter the DB), or just trying to get Ruby to work with it?
> >
> > By the sound of it, you have something in the DB that looks like
> > (forgive the crude notation)...
> >
> > Users <-- Expertise --> Educations
> >
> > 3 tables?
> >
> > Todd

>
> I have 3 tables
>
> users
> educations
> educactions_users
>
> The educactions_users is my join table, with the columns users_id and
> educations_id.
>
> A part of the user-edit-view looks like this, where the user can
> specified there educations. This works..after getting grey hair!!
>
> <h4>educations</h4>
> <% for education in Education.find(:all) %>
> <div>
> <%= check_box_tag "user[education_ids][]", education.id,
> @user.educations.include?(education) %>
> <%= education.name%>
> </div>
> <% end %>
>
> But.....
>
> In the show-view i want a list of educations wich the user has selected.
> I thought this.
>
> <h4>Educations</h4>
> <ul>
> <% @user.educations do |education| %>
> <li><%= education.name%></li>
> <% end %>
> </ul>
>
> This get empty value..
>
> This is my problem..
>
> Grtz..remco
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


Have you tried specifying the word "through" in your rails models.
This works for me, for example...

class User < ActiveRecord::Base
has_many :memberships
has_many :groups, :through => :memberships
end

class Group < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
end

class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :group
end

hth a little,
Todd

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h59.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16507 seconds with 14 queries