Forum: Ruby on Rails How Can I solve this?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D813cb05d2123405f3b5af5b16648251?d=identicon&s=25 Michael Mosser (Guest)
on 2006-01-04 19:56
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' %>

<select name=pricelist>
<%= options_from_collection_for_select (@customers, "id", "name") %>
</select>

<%= 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!!!!!
97cbca14d17274370cce501bbea7980a?d=identicon&s=25 Mike Harris (Guest)
on 2006-01-04 20:39
(Received via mailing list)
Assuming each customer has 0-1 pricelists, which you implied in your
e-mail, I believe:

@pricelist = Customer.find(params["pricelist"]).pricelist
17acfbd3a05cfb59e0543235cc0a813b?d=identicon&s=25 Joshua Susser (Guest)
on 2006-01-04 22:04
This is a job for Rails' associations. You should read up on them in the
ActiveRecord README.
http://api.rubyonrails.com/files/vendor/rails/acti...

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' %>
>
> <select name=pricelist>
> <%= options_from_collection_for_select (@customers, "id", "name") %>
> </select>
>
> <%= 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!!!!!
17acfbd3a05cfb59e0543235cc0a813b?d=identicon&s=25 Joshua Susser (Guest)
on 2006-01-04 22:09
Joshua Susser 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")
This topic is locked and can not be replied to.