activerecord

Rails updating attribute security: use a callback or attr_accessible?

I've got a website model that requires a user to verify ownership of the website. Thanks to stack overflow, I was able to find the solution for user ownership verification here: http://stackoverflow.com/questions/1842416/validate-website-ownership-in-rails After the model passes the verification test there is a verified attribute that ...

How do I test ActiveRecord references in Cucumber?

I am trying to create some cucumber features with references to other objects. I am running into some problems when I want to create relationships between objects and run tests on them. I haven't found any samples anywhere else. Let's say I have the following models: class Athlete < ActiveRecord::Base belongs_to :shoe_size validat...

How do I work around the has_many :through single association restriction?

Given the relations { proposals(id, ...), reviewers(id, ...), comments(id, user_id, proposal_id, ...), votes(id, user_id, proposal_id, ...) } how can I create an association from Vote to Comment? Each reviewer can vote once on a proposal "create unique index on votes(user_id, proposal_id)" and can comment many times. A...

Using Mysql native functions with ActiveRecord on development environment

Hi. I need to query my database using mysql native functions such as YEAR(date) or NOW().... This works on staging and on production environments but it won't work in my development environment because of SQLite's lack of support to these functions. Can you help? Thank you. ...

What is the purpose of :conditions on a belongs_to association?

Say I have the following association with an attached condition: belongs_to :admin_user, :class_name => 'User', :foreign_key => :admin_user_id, :conditions=> 'users.admin=TRUE' # or any variation with hash or array, {:admin => true}, etc. The API doc states that the :conditions option on belongs_to will: Specify the...

Can Rails Active Record understand two simultaneous relationships between two tables at once?

I have two tables, users and groups. A user can belong to many groups. A group can have many users. So I have created a have_and_belongs_to_many relationship between users and groups using a join table, groups_users. This all works as expected. What I would also like to do is specify an ACTIVE group for each user. Were it not for the h...

How to store different site parameters in database with Rails?

Hello. There are a lot of ways to store site preferences in database. But what if I need to manage datatypes. So some preferences will be boolean, others strings, others integers. How can I organize such store? ...

Mocking ActiveRecord relationship beheavior in RSpec tests

I've run into this problem with testing. Let's assume I have two models, User and Post, where user has_many :posts. I'm trying to spec out a code block that includes something like this: user = User.find(123) post = user.posts.find(456) I know how to mock out the User.find and user.posts parts. The user.posts mock returns an array of...

named_scope dependent on existence of association is breaking tests

User model: class User < ActiveRecord::Base named_scope :clients, :conditions => "roles_users.role_id = #{Role.find_by_name('client').id}" end When testing, throws error: Called id for nil, which would mistakenly be 4 -- if you really wanted (etc.) Role fixtures: client: name: client user: name: user Apparent problem:...

Rails Eager Loading Question Find(:all, :include => [:model])

Hello, I have a Topic and a Project model. I have a Many-to-many ass between them (HABTM one). In the Topic's Index Page, I want to display the number of projects that each topic have. So I have @topics = Topic.all(:include => [:projects]) In my controller, and so far so good. The problem is that the Project Model is so big that the ...

How do I include an instance method inside a before_save callback in a plugin?

Hi there, I'm creating a plugin and am having a hard time defining a before_save filter that calls an instance method I've just defined. Here's a quick sample: module ValidatesAndFormatsPhones def self.included(base) base.send :extend, ClassMethods end module ClassMethods def validates_and_formats_phones(field_names = [...

Hacking Active Record, how do I utilize the attributes method?

The attributes method returns a hash of all the attributes with their names as keys and the values of the attributes as values; I want to utilize this method, creating a new derivative of the update_attributes(attributes) method, lets call it jz_attributes(attributes). Update_attributes does this: def update_attributes(attributes) s...

Ruby on Rails active record syntax to query status updates in a twitter style application

I have a twitter style application whereby a user follows many people. This is achieved using a self-referential association. This all works very nice but my brain has just gone dead while trying to figure out the active record syntax needed to list status updates (posts) from people the user follows ordered by time in a single query. ...

ActiveRecord::HasManyThroughAssociationNotFoundError in UserController#welcome

I have a many to many relationship in rails. All database tables are named accordingly and appropriately. All model files are plural and use underscore to seperate words. All naming comventions are followed by ruby and rails standards. I'm using has many through in my models like this: has_many :users, :through => :users_posts #Post...

Return Object After Validation Failure

Is there a method to save an object but return the object if it fails the validation_uniqueness_of for a given field? For example, I have the following: class User has_many :words ... end class Word belongs_to :user validates_uniqueness_of :title ... end And I have a situation where I want to either return the Word object if...

How to use Expression.Or() in Castle ActiveRecord?

Please, explain me how to use Experssion.Or() in FindAll function. for example i have 2 conditions - Expression.Like("Text", "%coolstuff%") and Expression.Eq("FromInternet", false) how to use them together via "or"? ...

Set-up Many-to-Many in Rails with destroy allowed for only one of the many

I am trying to model a very simple system, but the Rails way of doing this is escaping me. I'd like to have two entities, a User and a Bet. A bet has two users and a user has many bets. I'd like the creating User of the bet to be able to mark it as resolved (do any form of update/delete on it). My initial thinking was to have a User ac...

Create missing auto increment attribute with rails migration

I'm writing a migration to convert a non-rails app into the right format for rails - one of the tables for some reason does not have auto increment set on the id column. Is there a quick way to turn it on while in a migration, maybe with #change_column or something? ...

Composite primary keys in Ruby on Rails

I cannot get the composite_primary_keys gem to work with my database. I have the following database tables: mysql> describe currencies; +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | curr_code | varchar...

Understanding Simple ActiveRecord Updates in Rails

I'm just diving into ActiveRecord and have not been able to find an answer to my question. If I am updating an object's attributes and then calling save()... will ActiveRecord save to the DB ONLY when the new values are different from the old values? Let's say I do something like this: thing_to_update = Thing.find_or_create_by_code(so...