in my app I have users from different countries and want to perform finds on them
I tried to do it like this in the index action
@fromcanada = User.find(:all, :country => 'canada')
but I got the error
Unknown key: country
However, so that leads me to ask, what can become a key? In my database schema file, I have a "country" column on the users table.
t.string "country"
Furthermore, when I did a find all
@users = User.all
I was able to do this
<%= user.country %></p>
Can you explain why my find all with conditions didn't work? and show me how I should have done it?
1 What is Active Record? Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.
Object Relational Mapping (ORM) is the technique of accessing a relational database using an object-oriented programming language. Object Relational Mapping is a way for our Ruby programs to manage database data by "mapping" database tables to classes and instances of classes to rows in those tables.
ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code.
Try this.
@fromcanada = User.find(:all, :conditions => { :country => 'canada' })
edit: As jason328 pointed out, the above answer is deprecated in 3.2, and an updated answer would be
@fromcanada = User.where(:country => 'canada')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With