How Can I solve this?


#1

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!!!


#2

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

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


#3

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

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!!!


#4

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