ruby on rails undefined method - ruby-on-rails

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

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.

Trouble accessing column value rails console

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

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.

Mongoid - how to get second record?

I can do
1.9.3p125 :122 > User.first
=> #<StandardUser _id: 4f849..
and
1.9.3p125 :124 > User.last
and
1.9.3p125 :125 > User.count
=> 5
but I can't find any way to get at the other records (2 thru 4).
User.skip(1).first returns you second document.

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?

Resources