Rails 3 - search by name instead of id

I am trying to learn RoR by creating my first application, the main inventory application. I tried searching, but could not find the correct syntax, so I thought I'd ask.

Here is my setup:

Model

class Item < ActiveRecord::Base
belongs_to :supplier

class Supplier < ActiveRecord::Base
has_many :items

Element controller

@items = Item.find_all_by_supplier_id '1'

This will return the data I want, but I was hoping to enter the provider name in the controller so that if the identifier does not match, it will still work correctly.

Hope enough information, thanks!

+5
source share
3 answers

You can invert the method bit and try:

@items = Supplier.find_by_name('THE NAME').items

Rails find_by_ * find_all_by_ * . , _id, , , . _id, .

, , , , :

Item.find(1).supplier
+17
@items = Item.joins(:suplier).where(:suplier => { :name => "Suplier name" })
0
Supplier.find_by_colname(params[:name])

colname - Supplier table column, . find_by_, , , ;). Doc

0

All Articles