Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access an object's (ActiveRecord::Relation) value by key in Ruby on Rails?

tl;dr How do I get the corresponding value with the key of an object?

I'm confused why

Atag.where(tag:'brand') gives me what I would call an object for lack of a better term: #<ActiveRecord::Relation [#<Atag id: 1, tag: "brand", created_at: "2015-01-31 04:29:20", updated_at: "2015-01-31 04:29:20">]>

But I'm having the basic difficult of accessing the corresponding value for the key :id.

Atag.where(tag:'brand').id and Atag.where(tag:'brand')[:id] and Atag.where(tag:'brand')(:id) all throw errors, while in this case I'm just trying to have the integer 1 returned.

I seem to be unable to ruby, nor find a succinct answer to this basic question with my google searching skills (or lack there of).

Thanks

like image 783
Laser Avatar asked Jan 31 '15 05:01

Laser


People also ask

What is ActiveRecord in Ruby on Rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.

What is an ActiveRecord relation object?

An instance of ActiveRecord::Base is an object that represents a specific row of your database (or might be saved into the database). Whereas an instance of ActiveRecord::Relation is a representation of a query that can be run against your database (but wasn't run yet).

How does foreign key work in Rails?

In Rails 5, adding foreign key constraints was added to have the database protect the integrity of associated data. Once a foreign key constraint is defined, your database will not allow you to remove records that are required by other tables.


1 Answers

Get the id of your tag = 'brand' with following query:

Atag.find_by(tag:'brand').id 

Check following variations:

Atag.find(1) 
#gives you the object with the Atag id = 1

Atag.find(100) #let's say this record does not exist then you will 
get ActiveRecord::RecordNotFound exception. 

Better option :

Atag.where(id: 1) 
#this returns you a relation and it's true you are trying to access
 only a single object.

Hence, you just need to modify it to :
Atag.where(id: 1).first 
#Above one will give you an object of Atag not an association result.
# to verfiy you can execute, Atag.where(id: 1).first.class

Atag.where(id: 999).first
 # In this case if there is no record found with id = 999, then it'll 
return  nil which can be easily handled than an exception found 
while using find method.

Get the same flavor using the dynamic finders.

Atag.find_by(id: 1) #gives the Atag with id 1 
Atag.find_by_id(1). # same as above.
Atag.find_by(id: 999) #if not found then simply returns nil. 
Atag.find_by(name: 'ruby') #return Atag object with name: 'ruby'
Atag.find_by_name('ruby') #same as above. 
like image 98
Ajay Avatar answered Oct 29 '22 22:10

Ajay