Trouble accessing column value rails console - ruby-on-rails

I'm trying to access the integer stored in a column called totalVotes in my UserVotes table but keep getting a No Method Error
I have been trying to debug from the console:
1.9.3-p327 :006 > uservote = UserVote.where("soundcloud_id = 68061927")
UserVote Load (0.2ms) SELECT "user_votes".* FROM "user_votes" WHERE (soundcloud_id = 68061927)
=> [#<UserVote id: 5, user_id: "1", party_profile_id: "1", soundcloud_id: "68061927", totalVotes: 0, created_at: "2013-02-19 04:57:58", updated_at: "2013-02-19 04:57:58">]
Good so far...
1.9.3-p327 :007 > uservote.totalVotes
NoMethodError: undefined method `totalVotes' for #<ActiveRecord::Relation:0x007febb49ab350>
Then it breaks and I'm not sure why. What is the best way for me to access the totalVotes value?

your issue is you're calling .totalVotes to an AR Relation. call .first first then you should have no problem.
>> uservote = UserVote.where("soundcloud_id = 68061927").first
>> uservote.totalVotes

Related

Could not find_by an object from database in Rails job

I have created a Ruby on Rails job (extending from ActiveJob::Base) that parses a CSV file and inserts its rows as records (Students) in a database. That is working as expected.
However, when I try to fetch another object from the DB (the Career for each Student, which is part of each CSV row as a pair (career_code, career_name)), I'm getting this error:
undefined method 'find_by' for Career:Class
(I have also tried using Career.where instead).
I find this quite strange, since I'm already saving my Student, which is also an ActiveRecord::Base child class.
Here's the relevant part of my job:
ActiveRecord::Base.transaction do
student_hash.keys.each do |k|
some_student = student_hash[k]
student = Student.new
student.csv_id = some_student.id
student.first_name = some_student.first_name
student.last_name = some_student.last_name
student.file_number = some_student.file_number
# student.career = Career.where(code: some_student.career_code)
student.career = Career.find_by code: some_student.career_code
puts student.save! # Why this works, and the above line doesn't?
end
end
And also, the desired output, as I can see it in the Rails console:
Loading development environment (Rails 4.2.4.rc1)
2.1.3 :001 > Career.where(code: 11)
Career Load (0.5ms) SELECT "careers".* FROM "careers" WHERE "careers"."code" = ? [["code", 11]]
=> #<ActiveRecord::Relation [#<Career id: 4, name: "Informática", created_at: "2015-09-30 22:05:07", updated_at: "2015-09-30 22:05:07", code: 11>]>
2.1.3 :002 > Career.where code: 11
Career Load (0.2ms) SELECT "careers".* FROM "careers" WHERE "careers"."code" = ? [["code", 11]]
=> #<ActiveRecord::Relation [#<Career id: 4, name: "Informática", created_at: "2015-09-30 22:05:07", updated_at: "2015-09-30 22:05:07", code: 11>]>
2.1.3 :003 > Career.find_by code: 11
Career Load (0.4ms) SELECT "careers".* FROM "careers" WHERE "careers"."code" = ? LIMIT 1 [["code", 11]]
=> #<Career id: 4, name: "Informática", created_at: "2015-09-30 22:05:07", updated_at: "2015-09-30 22:05:07", code: 11>
2.1.3 :004 >
Probably this is a really stupid question, but I'm quite a beginner using Rails. Do I need to import some sort of "Context" (this seems unlikely, since Student seems to be properly resolved)? I'm using Rails 4.2.4.rc1 by the way.
Thanks in advance
Any chance at that point in the code Career isn't an AR, but some other class? That would at least explain why the methods don't exist.

ruby on rails undefined method

sharvil#sharvil:~/railstut/blog$ rails c
Loading development environment (Rails 4.2.1)
2.2.2 :001 > q=Question.all
Question Load (0.5ms) SELECT `questions`.* FROM `questions`
=> #<ActiveRecord::Relation [#<Question questions_id: 1, question: "What is this", almuni_almuni_id: 1, category_category_id: 1>]>
2.2.2 :002 > q
=> #<ActiveRecord::Relation [#<Question questions_id: 1, question: "What is this", almuni_almuni_id: 1, category_category_id: 1>]>
2.2.2 :003 > q.question
NoMethodError: undefined method `question' for #<Question::ActiveRecord_Relation:0x000000045dc178>
from /home/sharvil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.1/lib/active_record/relation/delegation.rb:136:in `method_missing'
I am getting a error in accessing q.questions .. question is column in table.
pls help.
You need to do - q.first.question. Your q is a collection of questions not an instance of Question. And you called question method on the collection of questions, that's why you got the error.
In your case q is question collection
q.each do |que|
puts que.question
end

Why does rails change the type of the attribute _was value after it is set?

I have an attribute in my model that is stored as text but interpreted as a rational. I have this method to handle that:
def start
read_attribute(:start).to_r
end
When I set the start attribute to a new value, the start_was helper method returns a string, instead of a rational, but before I do so, it returns the correct value. Why?
Loading development environment (Rails 3.2.8)
1.9.3p194 :001 > d = Day.find(55)
Day Load (8.7ms) SELECT "days".* FROM "days" WHERE "days"."id" = ? LIMIT 1 [["id", 55]]
=> #<Day id: 55, date: "2012-03-30", start: "1/2", finish: "2/2", created_at: "2012-09-18 15:16:42", updated_at: "2012-09-19 08:20:41", day_year_id: 1>
1.9.3p194 :002 > d.start_was
=> (1/2)
1.9.3p194 :003 > d.start=0
=> 0
1.9.3p194 :004 > d.start_was
=> "1/2"
I think the reason is this method in ActiveModel (activemodel-3.2.8\lib\active_model\dirty.rb)
# Handle <tt>*_was</tt> for +method_missing+.
def attribute_was(attr)
attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr)
end
As you see, if attribute was not actually changed it just calls its own getter, in your case hitting your start method which does the transformation. But if the attribute is actually changed, it reads its raw value from the changed_attributes storage.

Nested objects and collection size

I run into a weird issue since I upgraded to Rails 3.2.
My application have some objects managed with nested_forms, but when i try to reach a collection's size after having built a new nested object, the nested object is taken in count.
For ex.:
1.9.3p0 :004 > e = Expense.last
Expense Load (22.6ms) (...)
=> #<Expense id: 1, (...)
1.9.3p0 :005 > e.comments.size
(0.3ms) SELECT COUNT(*) (...)
=> 0
1.9.3p0 :006 > e.comments.build
=> #<Comment id: nil, content: nil, commentable_id: 1, commentable_type: "Expense", created_at: nil, updated_at: nil, creator_id: nil>
1.9.3p0 :007 > e.comments.size
=> 1
In this case, I expect the e.expense.size to return 0 since the Commentobject has not been saved yet.
Building the comment creates ... 1 comment. It doesn't exist in the database (yet) so e.commment.count would return 0, but it exists in the application, so size returns 1. So that sort of makes sense.
But I do see there's a little potentially unexpected result here. If a comment doesn't exist in memory, it looks like Rails checks with the DB (the SELECT COUNT(*)...), whereas if it finds one in memory it does not.
I wonder what would happen if you had several existing comments already, then checked e.comments.size in that case?
Which version of Rails were you coming from that worked differently?

Error with "to_sql" on Rails 3 Beta 4

I'm testing Rails 3 beta 4 on Ruby 1.9.2-head, and when I start a
console and do:
Game.first.to_sql
I get this error:
ArgumentError: wrong number of arguments (0 for 1)
I know it can find the Game record, because when I type:
Game.first
it returns:
=> #<Game id: 1, name: "Galaga", created_at: "2010-06-19 11:02:37",
updated_at: "2010-06-19 11:02:37">
What am I missing? I just want to make the to_sql work in a very simple
case.
.
When you run Game.first you are returning a Game object, not a ActiveRecord::Relation object as you are expecting.
To do what you're trying to do, you'll need to do:
Game.limit(1).to_sql
This lets you run it without to_sql and return the object as you expected it, although it will be in an array, which then you can run .first on it like you wanted anyways.
irb(main):004:0> Game.limit(1).to_sql
=> "SELECT `games`.* FROM `games` LIMIT 1"
irb(main):005:0> Game.limit(1).class
=> ActiveRecord::Relation
irb(main):006:0> Game.limit(1)
=> [#<Game id: 1, name: "Galaga", created_at: "2010-06-19 11:02:37", updated_at: "2010-06-19 11:02:37">]
irb(main):007:0> Game.limit(1).first
=> #<Game id: 1, name: "Galaga", created_at: "2010-06-19 11:02:37", updated_at: "2010-06-19 11:02:37">
When you dig into the source, when you run .first on an ActiveRecord::Relation it runs the following (which is the same as I showed you):
def find_first
if loaded?
#records.first
else
#first ||= limit(1).to_a[0]
end
end

Resources