Reputation: 3339
I'm trying to get a record in DB by having these two lines in one of my models:
a = Arel::Table.new(:receivers)
e = Receiver.where(a[:name].matches('%afsane%'))
In my receivers table I have a row in which the name column equals to "afsane".I want to have that row's id in my model. but by printing the e in my model I've got this :
"========#<Receiver::ActiveRecord_Relation:0x0056094261eff8>============"
which gives the relation number but no params. When I run them in rails console I got this response:
#<ActiveRecord::Relation [#<Receiver id: 2, name: "afsane", created_at: "2016-09-27 09:24:16", updated_at: "2016-10-09 10:10:02">]>
I managed to have this in my model But I couldn't .
ps:Using Rails 5.
Have any Ideas ?
Upvotes: 0
Views: 212
Reputation:
I'm not sure what the variable a
looks like but the variable e
is a collection.
where
returns a collection which means you will have to iterate through e
in order to pull out some data.
Like so:
e.each do |data|
data.name
end
If you want to return only 1 result then you can simply do the following:
e = Receiver.where(a[:name].matches('%afsane%')).limit(1)
Note: I've kept the answer simple but there are more elegant solutions to this so I recommend you reading the ActiveRecord Rails Guides
Upvotes: 1