activerecord

Why is this an endless loop? ActiveRecord

class Account < ActiveRecord::Base after_update :give_user_credit, :on => :update def give_user_credit credit = User.current_user.credit + 3.8 User.current_user.update_attribute(:credit, credit) end end When I use this the server hangs and when I come back to the application after a full reboot my credit is in the £1000...

Updating protected attributes using update_all in a migration

Since you cannot use the normal 'update' and 'update_attribute' methods from ActiveRecord to update a protected attribute, is the following the best way to update an attribute for a single user? User.update_all("admin = true","id = 1") I'm guessing this doesn't lie in the 'best practice' category, so I'm just curious if there is a m...

Log your SQL in Rails application inside unit test

I want to install a logger so that I can dump all executed SQL of my rails application. Problem is, such logger is associated with AbstractAdapter which initialized very soon under test mode, and thus cannot be set by my initializer code. I try to put ActiveRecord::Base.logger = MyCustomLogger.new(STDOUT) in the end of environment.rb...

If I have a many to many relationship in Active Record, how do I add a new record?

Do I have to create a new instance of the join table and set the values or can I say something like student.classes.add(class) and that will work? Sincerely, tesmar ...

Why is SubSonic and ActiveRecord throwing an exception when updating a record? (Take 2)

I am experiencing the same error and stack trace as this question. However, I am using the latest code from github as of today (6/11/2010) and I am not having the compilation problems mentioned in the other answer. Other differences from the other poster: My class looks like this: public class ActiveRecordClass1 { private List<A...

Rails belongs_to issue in the views

Hi, Im having problems with an association in rails: Currently I have Post and User models, and the relationship is set this way: class User < ActiveRecord::Base attr_accessible :username, :name, :lastname has_many :posts end class Post < ActiveRecord::Base attr_accessible :title, :body belongs_to :user end However, in my ...

Rails How to get all the grandchildren of an ojbect.

I have 3 models User has_many :quetions has_many :corrections end Question has_one :correction belongs_to :user end Correction belongs_to :user belongs_to :question So if user Bob asks a question then user Terry can check it and if its wrong offer a correction. Lets stay with bob and assume he as kindly corrected 5 other users, i.e...

passing a parameter from an other model on ruby

I'm very newbie in ruby and need your help. I must save a "Topic" and make it like this : @topic = Topic.new(params[:topic]) But I would like to pass an other information to this topic. It has a field "community_id" that link it to a community. The logged user has this information on his table. How can I pass the "community_id" from...

ActiveRecord Create (not !) Throwing Exception on Validation

So I'm using ActiveRecord model validations to validate a form in a RESTful application. I have a create action that does: @association = Association.new and the receiving end of the form creates a data hash of attributes from the form parameters to save to the database using: @association = user.associations.create(data) I want...

Updating extra attributes in a has_many, :through relationship using Rails

I've managed to set up a many-to-many relationship between the following models Characters Skills PlayerSkills PlayerSkills, right now, has an attribute that Skills don't normally have: a level. The models look something like this (edited for conciseness): class PlayerSkill < ActiveRecord::Base belongs_to :character belongs_to ...

How to copy or clone model?

I have a model Book with attributes id, name, price. I have an instance of Book: b1 = Book.new b1.name = "Blah" b1.price = 12.5 b1.save I would like to copy b1, create another instance of the Product model. I'm tryid p1=b1.clone then p1.save but it didn't work. Any idea? And my environment is: Netbeans 6.9 RC2 JRuby 1.5.0 EDITED:...

Using two or more databases in rails project

Hi! I am using external Users database for different projects. Now I have got School model in my project, which has_many users and users has many schools. class User < ActiveRecord::Base establish_connection "#{RAILS_ENV}_tunnel" has_many :memberships has_many :schools, :through => :memberships end class School < ActiveRecord::...

How do I avoid a race condition in my Rails app?

Hi, I have a really simple Rails application that allows users to register their attendance on a set of courses. The ActiveRecord models are as follows: class Course < ActiveRecord::Base has_many :scheduled_runs ... end class ScheduledRun < ActiveRecord::Base belongs_to :course has_many :attendances has_many :attendees, :thr...

Dynamic class_name for has_many relations

I'm trying to make has_many relation with dynamic class_name attribute class Category < ActiveRecord::Base has_many :ads, :class_name => ( lambda { return self.item_type } ) end or class Category < ActiveRecord::Base has_many :ads, :class_name => self.item_type end But i got errors: can't convert Proc into String or undefi...

How to version SQL Server schema using VS 2005?

I am new to C# programming and am coming to it most recently from working with Ruby on Rails. In RoR, I am used to being able to write schema migrations for the database. I would like to be able to do something similar for my C#/SQLServer projects. Does such a tool exist for the VS 2005 toolset? Would it be wise to use RoR migrations...

belongs_to with a custom class_name not producing proper foreign key in Rails 3

I am updating an application to Rails 3 and I am having trouble creating a custom foreign key. I have something like this: class Product < ActiveRecord::Base belongs_to :owner, :class_name => 'User' ... end class User < ActiveRecord::Base has_many :products ... end class ProductsController < ApplicationController before_filter ...

rails: Get all items tagged x AND y AND z

I've got two models: Item and Tag. Both have a name attribute. I want to find items tagged with several tags. class Item < ActiveRecord::Base has_many :tags validates_presence_of :name end class Tag < ActiveRecord::Base belongs_to :item validates_presence_of :name end Given a list of tag ids, I can easily enough get the list ...

Rails find_or_create by more than one attribute?

There is a handy dynamic attribute in active-record called find_or_create_by: Model.find_or_create_by_<attribute>(:<attribute> => "") But what if I need to find_or_create by more than one attribute? Say I have a model to handle a M:M relationship between Group and Member called GroupMember. I could have many instances where member_id ...

How to skip callbacks on Mongoid Documents?

My question is similar to this one http://stackoverflow.com/questions/1342761/how-to-skip-activerecord-callbacks but instead of AR I'm using Mongoid, It seems like that isn't implemented yet in the current version of Mongoid, so I'd like to know what should be an elegant solution to implement it. (if necessary). ...

ActiveRecord :has_many associations and === operator

Can someone explain me this Ruby on Rails puzzle? class Post < ActiveRecord::Base has_many :comments end Post.first.comments.class => Array Array === Post.first.comments => false Array === [ 1 ] => true ...