# How Can I solve this?

**URL:** <https://www.ruby-forum.com/t/how-can-i-solve-this/56159>\
**Category:** Rails\
**Created:** [January 4, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/how-can-i-solve-this/56159 "2006-01-04T19:56:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Mosser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Michael\_Mosser](https://www.ruby-forum.com/u/Michael_Mosser)\
**Post date:** [January 4, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/how-can-i-solve-this/56159/1 "2006-01-04T19:56:28Z")

</div>

this may be an easy question, but i am unable to solve it:

i want to take the selected choice and process it in the controller:

TEMPLATE:  
\<%=start\_form\_tag :action =\> ‘get\_article’ %\>

\<%= options\_from\_collection\_for\_select (@customers, "id", "name") %\>

\<%= submit\_tag “Get” %\>

\<%= end\_form\_tag %\>

CONTROLLER:  
def get\_article  
@pricelist=Pricelist.find(??)  
@articles=Article.find(@pricelist.pricelist\_id)  
end

i want to take the selected id (handed over as a parameter) from  
customer and get in pricelist the record where  
customer.id=pricelist.customer\_id

thank you for your HELP!!!

---

<div class="post-metadata">

**Author:** ![Michael\_Mosser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Michael\_Mosser](https://www.ruby-forum.com/u/Michael_Mosser)\
**Post date:** [January 4, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/how-can-i-solve-this/56159/2 "2006-01-04T20:39:38Z")

</div>

Assuming each customer has 0-1 pricelists, which you implied in your  
e-mail, I believe:

@pricelist = Customer.find(params[“pricelist”]).pricelist

---

<div class="post-metadata">

**Author:** ![Michael\_Mosser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Michael\_Mosser](https://www.ruby-forum.com/u/Michael_Mosser)\
**Post date:** [January 4, 2006, 10:04pm UTC](https://www.ruby-forum.com/t/how-can-i-solve-this/56159/3 "2006-01-04T22:04:46Z")

</div>

This is a job for Rails’ associations. You should read up on them in the  
ActiveRecord README.  
[http://api.rubyonrails.com/files/vendor/rails/activerecord/README.html](http://api.rubyonrails.com/files/vendor/rails/activerecord/README.html)

In your model classes you should have some class methods like…

def Customer \< ActiveRecord::Base  
has\_one :pricelist  
…  
end

def Pricelist \< ActiveRecord::Base  
belongs\_to :customer  
…  
end

I’m not sure what to do about Article because you didn’t give enough  
info about its relationships to other models. Anyway, after you have  
those associations set up, Rails provides methods like

my\_customer = Customer.find(…)  
my\_pricelist = my\_customer.pricelist

That second line is just like doing a Pricelist.find(:first, :condition  
=\> “#{my\_customer.id} = customer.id”). Rails provides many of those  
types of methods, and you should learn to use them because they make  
your life much easier.

Michael Mosser wrote:

> this may be an easy question, but i am unable to solve it:
> 
> i want to take the selected choice and process it in the controller:
> 
> TEMPLATE:  
> \<%=start\_form\_tag :action =\> ‘get\_article’ %\>
> 
> \<%= options\_from\_collection\_for\_select (@customers, "id", "name") %\>
> 
> \<%= submit\_tag “Get” %\>
> 
> \<%= end\_form\_tag %\>
> 
> CONTROLLER:  
> def get\_article  
> @pricelist=Pricelist.find(??)  
> @articles=Article.find(@pricelist.pricelist\_id)  
> end
> 
> i want to take the selected id (handed over as a parameter) from  
> customer and get in pricelist the record where  
> customer.id=pricelist.customer\_id
> 
> thank you for your HELP!!!

---

<div class="post-metadata">

**Author:** ![Michael\_Mosser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Michael\_Mosser](https://www.ruby-forum.com/u/Michael_Mosser)\
**Post date:** [January 4, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/how-can-i-solve-this/56159/4 "2006-01-04T22:09:49Z")

</div>

Joshua S. wrote:

> my\_customer = Customer.find(…)  
> my\_pricelist = my\_customer.pricelist
> 
> That second line is just like doing a Pricelist.find(:first, :condition  
> =\> “#{my\_customer.id} = customer.id”). Rails provides many of those  
> types of methods, and you should learn to use them because they make  
> your life much easier.

Oops, that should have been  
Pricelist.find(:first, :condition \> =\> “#{my\_customer.id} =  
customer\_id”)
