activerecord

Implicitly create associated model from string

I have a model Post that has_many :tags I want to do: Post.create({:tags => ['tag1', 'tag2']}) How can I make that work? ...

GET params in ruby-on-rails project - best practices?

I've inherited a little rails app and I need to extend it slightly. It's actually quite simple, but I want to make sure I'm doing it the right way... If I visit myapp:3000/api/persons it gives me a full list of people in XML format. I want to pass param in the URL so that I can return users that match the login or the email e.g. yapp:30...

ActiveRecord validates... custom field name.

I would like to fix up some error messages my site generates. Here is the problem: class Brand < ActiveRecord::Base validates_presence_of :foo ... end My goal is to make a message "Ticket description is required" instead of "Foo is required" or may not be blank, or whatever. The reason this is so important is because lets say...

Restoring Typus plugin after changing DB Schema

I installed Typus plugin (http://intraducibles.com/projects/typus) for my app and I love it. But along the development of the app I frequently do migrations and change the DB Schema and relationships, and then the plugin fails. Is there a way to re-generate the plugin with the new schema? Thanks! ...

Serializing ActiveRecord objects without storing their attributes?

I'm working on a problem where I need to store serialized hierarchies of Ruby objects in the database. Many of the objects that will need to be saved are ActiveRecord objects with a lot of attributes. Instead of saving the entire objects and then refreshing their attributes from the DB when I load them (in case they changed, which is l...

nhibernate activerecord linq Contains problem

Hi, I am having problems with the following query in Castle ActiveRecord 2.12: var q = from o in SodisceFMClientVAR.Queryable where taxnos2.Contains(o.TaxFileNo) select o; taxNos2 is an array of strings. When run I get an exception: + InnerException {"Index was out of range. Must be non-negative and ...

How to configure a has_many association with non-ActiveRecord model

My Rails app has a normal ActiveRecord "Account" model stored in the database. The model will store the URL to a remote XML file which defines some other objects. For example, the Account has_many :galleries but the Gallery model is simply defined by nodes in the XML document. So how do I get /accounts/1/galleries to show the galleries...

How do I add a condition to all ActiveRecords Queries for a particular model?

I am using the sentient_user gem to have access to the current_user object in my application. I want to override the default ActiveRecordBase queries to scope them to the current user. For instance, I don't want my users looking at, deleting, modifying other user's orders. I feel like I should be able to override a single (or couple) A...

Mapping legacy database columns in a rails project

We are creating a rails application for an already existing database. We need to map some database tables together. Suppose we have three tables: event, event_groups and event_to_groups. There are some events, there are some groups, and each event can be assigned to one or more groups. How do I model this relation in rails? eg: Ex...

How to override the attr_protected?

I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car < Automobile end class Truck < Automobile end class User < ActiveRecord::Base has_many :automobiles accepts_nested_attributes_for :automobiles end I am creating a list of automobiles for a user. For each automobile, the UI sets the type fi...

Problems using a legacy database with a new rails application

What are the usual problems / stumbling blocks / issues / drawbacks when using a legacy database with a new rails application? We have to decide between using an old database or writing migration scripts to bring data from old database to new database following rails conventions. What would you suggest? ...

How can I override the attribute assignment in an active record object?

I know you can do this with virtual attributes, but what if the column actually exists? For example, my model has a raw_topic column. When raw_topic is set, I want artist and song_title to be set based off of raw_topic's contents. Ideally, I'd like to override the raw_topic= method, but rails doesn't seem to like that. What's the prope...

Rails paginate existing array of ActiveRecord results

Hello, I generally use will_paginate for the pagination in my app, but have hit a stumbler on my search feature. I'm using Thinking Sphinx for doing my full-text search, which returns results paginated. The problem I'm having is that after I've received the results from Thinking Sphinx, I need to merge them with some other results and r...

ActiveRecord date format

I've run into a spot of bother with date formats in our Rails application. I have a date field in our view which I want to be formatted as dd/mm/yy. This is how the user will expect to enter their dates, and the datepicker control uses this format. However, Active Record seems to be expecting mm/dd/yy. If I enter 01/03/2010, this get...

Unable to read values from object returned from ActiveRecord.find

I make the following call to the DB. @patientRegistration = PatientRegistration.find(:all, :conditions=>["name = '#{patientName}'"]) This searches for a patient registration based on a given name. I get a valid @patientRegistration object. When I invoke @patientRegistration.inspect it prints correctly all the...

Active Record two belongs_to calls or single table inheritance

In linking a sports event to two teams, at first this seemed to make sense: events - id:integer - integer:home_team_id - integer:away_team_id teams - integer:id - string:name However I am troubled by how I would link that up in the active record model: class Event belongs_to :home_team, :class_name => 'Team', :foreign_ke...

rails active record - Advanced find

I have the following model setup - a User is interested in projects in many project Categories. Each Project has many categories. Like so: class User has_many :projects has_and_belongs_to_many :project_categories class Project belongs_to :user has_and_belongs_to_many :project_categories class ProjectCategory has_and_belongs_...

How to convert attribute name to string?

Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname, :lastname, :email end ... some_helper_method(attrib) ... def Now I would like to pass someuser.firstname to helper and I would like to get both the value and the attribute name, for example: some_helper_method(someuser.firstname)...

save object associate to another object automatically

Hi i have these classes: class Core < ActiveRecord::Base belongs_to :resource, :polymorphic => true belongs_to :image, :class_name => 'Multimedia', :foreign_key => 'image_id' end class Place < ActiveRecord::Base has_one :core, :as => :resource end If i try do launch this: a = Place.find(5) a.name ="a" a.core.i...

has_many :through on self?

I have a User model. A user can be either a dj, a club, or a clubber (this is controlled by the User#account_type attribute). A club can have many djs, and a dj can have many users: enumerated_attribute :account_type, %w(^clubber dj club), :nil => false do label :clubber => "Clubber" label :dj => "DJ" label :club => "Club" end ...