Hi to all,
I am having problems getting a search to work. I have a search form with
one text field for search linking to missing_children_controller.rb.
This text field gets the name of a child to find matches.
What I’m trying to do is to get the search parameter (which would be the
name of the child) to first search through the children table in the
database to pull any matches. Then I feed the result of the matches
(@children.id) to see if any of the previous matches from the search are
actually missing children (in the missing_children table in the
database).
Models Associations:
Child
has_many :missing_children
MissingChild
belongs_to :child
Database Tables:
children
id:int
name:string
missing_children
id:int
child_id:int
missing_children_controller.rb
@children = Child.find(:all, :conditions => ["name LIKE ?",
"%#{params[:search]}%"])
@missing_children = MissingChild.find(:all, :conditions => ["child_id =
?", @children.id])
The above code generates a random id from @children.id (for example:
18927530). When it should generate the actual child id. However if I
replease @children.id in the above code to 1 (which is an actual child
id in the children database and also listed in the child_id in the
missing_children database, it DOES work.
Is this code correct? Maybe there’s a better way to do this?
Any ideas on why this is not working? I’m pretty new to Ruby on Rails
but I think this database structure is the right way to go. In PHP I
normally wouldn’t have a seperate database for missing_children, I would
just add a status:string to the children database and put fill it with
“Lost”.
Anyway, I have a feeling I’m doing something wrong so any input is
appreciated. Thanks in advance!
-Tony