activerecord

insert a value into an integer column without updating it, but adding it's contents.

hello i have: ActiveRecord::Base.connection.execute "UPDATE ventas SET costo_de_compra = #{@nuevo_costo} WHERE id = #{@vid};" but this updates that column value every time it's recursed, what i want is just to insert that value to the already stablished values in that column... in proper instance i want to add the values to an intege...

Unfortunately this works: ROR Comparison in Activerecord - update.

Unfortunately this mess works: Do you have suggestings for cleaning up this code: I'm trying to use a activerecord to compare two table columns, "needed" and "amount" then update a boolean column, depending on the returned data. Totally breaking do-not-repeat coding. def update @inventory = Inventory.find(params[:id]) respond_...

Order products by association count

Hello everyone i have Class Product has_many :sales end Class Sale belongs_to :product end How do i get the most sold products.. (Product find all.. order by .. ventas..) ? ...

order products by association count belongs_to

hi i have Class Sale belongs_to :product, :accessible => true belongs_to :brand, :accessible => true end Class Product has_many :sales belongs_to :brand end Class Brands has_many :products has_many :sales end How do i get the brands that have the most product sales? ...

Keep record of deleted items

Hello i have a rails app that handles sales, right now, what i need is to be able to delete the sale in order to keep accounting clear, but log somewhere else, the details of that record. I am thinking i may need to create a logger, but have no idea how, or maybe another object who gets created on the destroy of the sale. Thanks in adv...

When do I use save!, create! and update_attributes! in Rails?

I'm trying to figure out when to use the bang! versions for saving and updating records? I've read and heard that you don't need them if you're just saving one record or updating a single attribute, if you're confident nothing should go wrong, or to always use them outside of a controller. I guess I'm paranoid about having multiple thing...

ActiveRecord sum errors with postgresql

I am using AR's sum method for a query and seeing this error when using PostgreSQL: PGError: ERROR: function sum(character varying) does not exist LINE 1: SELECT sum("assets".asset_file_size) AS sum_asset_file_size ... HINT: No function matches the given name and argument types. You might need to add explicit type casts. SELECT sum("as...

Ruby on Rails ActiveRecord Validation

I would like to validate attributes in a function like this class User < ActiveRecord::Base validate :check_name( :name ) def check_name( name ) ... if name is invalid ... self.errors.add( :name, 'Name is invalid') end end Can you please write the right code? Please explain the functionality why... THX! ...

using a non-integer id column in ActiveRecord

Are there any gotchas to using a non-integer column for the id in an ActiveRecord model? We're going to be using replicated databases, with copies of a Rails app writing to those databases in different datacenters. I'm worried that with normal IDs we'll get collisions between newly created rows in different datacenters. Our DBA has su...

Equivalent of SqLite Blob type in Subsonic?

In one SqLite table, I have a BLOB column for saving images (or binary data as a matter of fact). The table is Documents. Strangely, in Subsonic's ActiveRecord's Documents class, the type of that column shows as STRING which doesn't make sense. It should be byte array. Right? What am I missing here? How do I map SqLite BLOB column in S...

Rails ActiveRecord: Locking down attributes when record enters a particular state

Wondering if there’s a plugin or best way of setting up an ActiveRecord class so that, for example, when a record enter the "published" state, certain attributes are frozen so that they could not be tampered with. ...

ROR ActiveRecord attribute handling with a callback before_update

This code produces an ActiveRecordError: Callbacks must be a symbol denoting the method to call, a string to be evaluated, a block to be invoked, or an object responding to the callback method." before_update :check_instock, :unless => Proc.new { |inventory| inventory.needed.nil? } def check_instock if needed < amount ...

How to set Default format for activerecord fields of type string?

Here's an easy one: How do I go about setting the default format for a string field in ActiveRecord? I've tried the following: def phone_number_f "...#{phone_number}format_here..." end But I'd like to keep the method name the same as the field name. ...

link_to issue with inherited Active Record class.

Here are the classes as I have them set up: class Stat < ActiveRecord::Base belongs_to :stats_parent end class TotalStat < Stat belongs_to :stats_parent end #The StatsParent class is just to show how I use the relation. class StatsParent < ActiveRecord::Base has_one :total_stat has_many :stats end For the Stats Cont...

Adding Variables to AR Object

How can I add a variable to a set of objects returned by ActiveRecord? I've looked around and none of the methods I've seen seem to work. Thanks in advance! ...

Could not find the association problem in Rails

I am fairly new to Ruby on Rails, and I clearly have an active record association problem, but I can't solve it on my own. Given the three model classes with their associations: # application_form.rb class ApplicationForm < ActiveRecord::Base has_many :questions, :through => :form_questions end # question.rb class Question < ActiveR...

rails db neutral pivot table or crosstab

Does anyone know of a way to build a pivot table using activerecord which would be remotely DB neutral? I've tried to avoid using find_by_sql and DB specific queries but for a pivot table or crosstab query I have no idea how to do it in a way which is not specific to say MySQL. IE my mySQL find_by_sql breaks on a postgresql DB. I foun...

Model relation issue with Ruby on Rails

I have a few models: class StatsParent < ActiveRecord::Base class CourseStat < StatsParent class PlayerCourseStat < CourseStat I have the Course model set up as such: class Course < ActiveRecord::Base has_one :course_stat has_many :player_course_stats def update_stats(plyr_rnd) puts self.course_stat # this puts #<PlayerC...

ActiveRecord fundamentally incompatible with composite keys?

I have been attempting to use subsonic for a project on which I'm working. All was going quite well until I encountered a link table with a composite primary key. That is a key made up of the primary keys of the two tables it joins. Subsonic failed to recognize both keys which was problematic. I was going to adjust subsonic to suppor...

attr_accessible in rails Active Record

When I use the attr_accessible to specify which fields from my Model I will expose, is it true for script/console as well? I mean something that I didn't specify as attr_accessible won't be accessible as well through console ? ...