Method Gives Activerecord::Relation Error

Method gives ActiveRecord::Relation error?

The methods total and today are defined on a model object. When you call current_user.purchases you associate to a relation which is has_many which means that in the end it's the Array. Therefore you can't call Purchase methods on it. You can do it this way:

  class Purchase < ActiveRecord::Base
# ...
scope :today, lambda { joins(:unit_price, :price).
where(:price => {:date => Date.today},
:unit_price => { :date => Date.today }) }
def total
self.price.sum(:amount) + self.unit_price.sum(:amount)
end
end

And then call it like this:

   <%= number_to_currency(current_user.purchases.today.inject{ |sum, p| sum + p.total }) %>

Scope can be called on a relation.

You need to call inject since again total is Purchase method and the relation is Array so you need to aggregate the array. In order to keep the code clean you may want to define a today_purchases_total method on User so then you can call it like:

   <%= number_to_currency(current_user.today_purchases_total) %>

For more info about this you may refer to http://guides.rubyonrails.org/active_record_querying.html#scopes and all RoR guides in general.

Ruby on rails ActiveRecord Relation not working?

You are getting an association here:

 Game.where(:appid => game["appid"])

... this is realized as an array of objects (even if the sql returns no records, then it is still an array, although it is empty).

You need to select one of them ... probably the first, like this:

 Game.where(:appid => game["appid"]).first.achievements.new

Or you can run through the values:

 Game.where(:appid => game["appid"]).each { |game| game.achievements.new }

or some such.

rails 3.0.3 - ActiveRecord::Relation, undefined method error

One user has many events. In your code, it looks like you're trying to access events on @users, which presumably would be more than one user.

You probably want to do something like:

@users.each do |user|
user.events.each do |event|
...
end
end

or:

 @user.map(&:events).flatten.each do |event|
...
end

Additionally, you don't need to specify attr_accessible on any attributes that are columns in your database.

Rails undefined method for ActiveRecord::Relation error calculating cart items

Try this,
@cart_items.shop_cart.total_cart_price it's a cart method.

Rails ActiveRecord - retrieve data from association and relation error

When you're using has_and_belongs_to_many association, you should have compositions_materials table with composition_id and material_id columns (both integer).

More info about this kind of association here:

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many

undefined method for ActiveRecord::Relation

Well, you are getting back an object of ActiveRecord::Relation, not your model instance, thus the error since there is no method called lastname in ActiveRecord::Relation.

Doing @medicalhistory.first.lastname works because @medicalhistory.first is returning the first instance of the model that was found by the where.

Also, you can print out @medicalhistory.class for both the working and "erroring" code and see how they are different.

Rails 2: Model.find(1) gives ActiveRecord error when id 1 does not exist

This is the expected behavior. I think David explains this the best himself, so here is a quote from Ruby, S., Thomas, D. & Hansson, D.H., 2009. Agile Web Development with Rails, Third Edition Third Edition., Pragmatic Bookshelf (p.330).

When you use a finder driven by
primary keys, you’re looking for a
particular record. You expect it to
exist. A call to Person.find(5) is
based on our knowledge of the people
table. We want the row with an id of
5. If this call is unsuccessful—if the record with the id of 5 has been
destroyed—we’re in an exceptional
situation. This mandates the raising
of an exception, so Rails raises
RecordNotFound.

On the other hand,
finders that use criteria to search
are looking for a match. So,
Person.find(:first,
:conditions=>"name=’Dave’") is the
equivalent of telling the database (as
a black box) “Give me the first person
row that has the name Dave.” This
exhibits a distinctly different
approach to retrieval; we’re not certain up front that we’ll get a result.
It’s entirely possible the result set
may be empty. Thus, returning nil in
the case of finders that search for
one row and an empty array for finders
that search for many rows is the
natural, nonexceptional response.



Related Topics



Leave a reply



Submit