# Searching two form at once

**URL:** <https://www.ruby-forum.com/t/searching-two-form-at-once/185931>\
**Category:** Rails\
**Created:** [March 29, 2010, 12:33pm UTC](https://www.ruby-forum.com/t/searching-two-form-at-once/185931 "2010-03-29T12:33:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![newnw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/919ad9/32.png) [@newnw](https://www.ruby-forum.com/u/newnw)\
**Post date:** [March 29, 2010, 12:33pm UTC](https://www.ruby-forum.com/t/searching-two-form-at-once/185931/1 "2010-03-29T12:33:53Z")

</div>

Hi,

I have a controllers called x and y. I want to search and display “y”  
tables data using X’s primary key, this key is a foreign key for Y.

eg-

when I click "show " option in the X table’s index , I want to display  
X’s data and Y’s data those are relavant to the X’s primary key.( need  
to display all in one form).

I would appreciate very much if any one can guide me or give me a link  
to find the answer for this.

Many thanks.

---

<div class="post-metadata">

**Author:** ![newnw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/919ad9/32.png) [@newnw](https://www.ruby-forum.com/u/newnw)\
**Post date:** [March 29, 2010, 12:52pm UTC](https://www.ruby-forum.com/t/searching-two-form-at-once/185931/2 "2010-03-29T12:52:01Z")

</div>

On 29 March 2010 11:33, Nipuna W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I have a controllers called x and y. I want to search and display “y”  
> tables data using X’s primary key, this key is a foreign key for Y.

That does not make sense to me unless you mean that the id field in xs  
table is the same as the id field in ys table. In that case why not  
just combine the tables?

Probably I do not understand exactly what you mean. Is so please give  
the relationships between the models (has\_many, belongs to etc)

Colin

---

<div class="post-metadata">

**Author:** ![newnw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/919ad9/32.png) [@newnw](https://www.ruby-forum.com/u/newnw)\
**Post date:** [March 29, 2010, 1:13pm UTC](https://www.ruby-forum.com/t/searching-two-form-at-once/185931/3 "2010-03-29T13:13:57Z")

</div>

Hi Colin,

Thank you very much for your quick reply. I’m just sitting in front of  
the computer and thinking what to do , because I’m new to ruby and  
rails.

here is the explanation.

x has zero or more records in the y table. and y belongs to x.  
hope this will help.x table id is name y table id is Itname. but both  
are reprecenting the same data.

I appreciate if you can answer for this ASAP.

Thanks.

Colin L. wrote:

> On 29 March 2010 11:33, Nipuna W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi,
> > 
> > I have a controllers called x and y. I want to search and display “y”  
> > tables data using X’s primary key, this key is a foreign key for Y.
> 
> That does not make sense to me unless you mean that the id field in xs  
> table is the same as the id field in ys table. In that case why not  
> just combine the tables?
> 
> Probably I do not understand exactly what you mean. Is so please give  
> the relationships between the models (has\_many, belongs to etc)
> 
> Colin

---

<div class="post-metadata">

**Author:** ![newnw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/919ad9/32.png) [@newnw](https://www.ruby-forum.com/u/newnw)\
**Post date:** [March 29, 2010, 3:20pm UTC](https://www.ruby-forum.com/t/searching-two-form-at-once/185931/4 "2010-03-29T15:20:42Z")

</div>

On 29 March 2010 12:13, Nipuna W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> are reprecenting the same data.  
> I think maybe you need get some basic rails knowledge. I suggest you  
> have a look at the rails guides at [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/),  
> Start with Getting Started (obviously). To help with your problem  
> then look at ActiveRecord relationships. Then look through the  
> others. It is not that I do not want to answer your question  
> directly, but I think that you have some fundamental ideas wrong which  
> would be sorted by working through the guides.

Colin

---

<div class="post-metadata">

**Author:** ![newnw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/919ad9/32.png) [@newnw](https://www.ruby-forum.com/u/newnw)\
**Post date:** [March 30, 2010, 9:33am UTC](https://www.ruby-forum.com/t/searching-two-form-at-once/185931/5 "2010-03-30T09:33:28Z")

</div>

On 29 March 2010 23:44, Nipuna W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I didn’t mean both controllers reprecenting the same data, I meaned X’s  
> primary key is a foreing key in Y. need to seach both tables at onces  
> and display both tables record in a form.

Could you insert your comments after the relevant part of the mail you  
are responding to please, it is easier to follow the thread then.  
Thanks

I think we had better start again, show me the relationships you have  
defined between your models, such as  
x has many y, y belongs to x, or whatever it is, and what fields are  
in the tables (just the important ones)

Colin

---

<div class="post-metadata">

**Author:** ![newnw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/919ad9/32.png) [@newnw](https://www.ruby-forum.com/u/newnw)\
**Post date:** [March 30, 2010, 12:44am UTC](https://www.ruby-forum.com/t/searching-two-form-at-once/185931/6 "2010-03-30T00:44:49Z")

</div>

Hi,

I didn’t mean both controllers reprecenting the same data, I meaned X’s  
primary key is a foreing key in Y. need to seach both tables at onces  
and display both tables record in a form.  
Thanks.

Colin L. wrote:

> On 29 March 2010 12:13, Nipuna W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > are reprecenting the same data.  
> > I think maybe you need get some basic rails knowledge. I suggest you  
> > have a look at the rails guides at [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/),  
> > Start with Getting Started (obviously). To help with your problem  
> > then look at ActiveRecord relationships. Then look through the  
> > others. It is not that I do not want to answer your question  
> > directly, but I think that you have some fundamental ideas wrong which  
> > would be sorted by working through the guides.
> 
> Colin

---

<div class="post-metadata">

**Author:** ![newnw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/919ad9/32.png) [@newnw](https://www.ruby-forum.com/u/newnw)\
**Post date:** [March 30, 2010, 1:44pm UTC](https://www.ruby-forum.com/t/searching-two-form-at-once/185931/7 "2010-03-30T13:44:24Z")

</div>

On 30 March 2010 12:03, Nipuna W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> order id is common for both table, that is the link. One order has one  
> or more items.
> 
> want to show relevant item/s details (relevant to particular order)  
> when I click the show button in the order index form. I want to display  
> both order and relevant item/s data in one form.

Did you have a look at the guides I suggested? You do not appear to  
be following the rails conventions. For example the order table  
should not have a field called order\_id, it should just be id. Also  
the tables should be called orders and items, not order and item.  
You have not told me the relationships (see the guide on ActiveRecord  
relationships as I suggested). It is presumably something like order  
has\_many items and item belongs\_to order. If you set this up  
correctly then if you have an order in @order the items are  
automatically available in the array @order.items. Please spend a few  
of days working through some of the guides and trying examples from  
the guides. It will be time well spent.

Colin

---

<div class="post-metadata">

**Author:** ![newnw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/919ad9/32.png) [@newnw](https://www.ruby-forum.com/u/newnw)\
**Post date:** [March 30, 2010, 1:03pm UTC](https://www.ruby-forum.com/t/searching-two-form-at-once/185931/8 "2010-03-30T13:03:43Z")

</div>

Colin L. wrote:

> On 29 March 2010 23:44, Nipuna W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi,
> > 
> > I didn’t mean both controllers reprecenting the same data, I meaned X’s  
> > primary key is a foreing key in Y. need to seach both tables at onces  
> > and display both tables record in a form.
> 
> Could you insert your comments after the relevant part of the mail you  
> are responding to please, it is easier to follow the thread then.  
> Thanks
> 
> I think we had better start again, show me the relationships you have  
> defined between your models, such as  
> x has many y, y belongs to x, or whatever it is, and what fields are  
> in the tables (just the important ones)
> 
> Colin

Hi Colin,

Thanks for keeping in touch.  
eg - order tabel- order id, desc, date, …  
item table- itemno,name,price,oder id…

order id is common for both table, that is the link. One order has one  
or more items.

want to show relevant item/s details (relevant to particular order)  
when I click the show button in the order index form. I want to display  
both order and relevant item/s data in one form.

Thanks.
