activerecord

Remove duplicate records/objects uniquely identified by multiple attributes

I have a model called HeroStatus with the following attributes: id user_id recordable_type hero_type (can be NULL!) recordable_id created_at There are over 100 hero_statuses, and a user can have many hero_statuses, but can't have the same hero_status more than once. A user's hero_status is uniquely identified by the combination of r...

Rails nested attributes with a join model, where one of the models being joined is a new record

I'm trying to build a grid, in rails, for entering data. It has rows and columns, and rows and columns are joined by cells. In my view, I need for the grid to be able to handle having 'new' rows and columns on the edge, so that if you type in them and then submit, they are automatically generated, and their shared cells are connected to ...

How to handle ids and polymorphic associations in views if compound keys are not supported?

I have a Movie plan table: movie_plans (id, description) Each plan has items, which describe a sequence of movies and the duration in minutes: movie_plan_items (id, movie_plan_id, movie_id, start_minutes, end_minutes) A specific instance of that plan happens in: movie_schedules (id, movie_plan_id, start_at) However the schedule ...

Rails 2.3.5: How to handle this type of validation

The use case is simple. I allow users to enter in an expiration field which needs to be between 1 and 15 into a form. The model takes that number and converts it into a datetime (such as adding 15 days from today) and stores it in the database. What's the correct way to actually validate that though? Do I validate against the datetime ...

Saving multiple select form data in db

Hi. I've got form for some model A, which has got few fields: tile description ... colors colors are selected from multiple select and options are ['red', 'green', 'blue', 'yellow']. User can choose colors as many as he wants. I don't think that making Color model and has_many relationship is good solution here to store colors data...

Rails: How do I validate against this code that I put into the lib/ directory?

Having a bit of difficulty finding out the proper way to mix in code that I put into the lib/ directory for Rails 2.3.5. I have several models that require phone validation. I had at least three models that used the same code, so I wanted to keep things DRY and moved it out to the lib/ directory. I used to have code like this in each mo...

Ruby on rails active-record generated SQL on Postgres

Dear all, Why does Ruby on rails generated more queries in the background on Postgres than MySQL? I haven't tried deploying Rails on production with Postgres yet, but I am just afraid this generated queries would affect the performance. Do you find Rails with Postgres is slower than MySQL, knowing that it produce more query on the backg...

How do i solve this 3 model activerecord problem?

class student < ActiveRecord::Base has_many :projects def has_a_teacher_by_the_name_of(name) self.projects.any? { |project| project.teacher.exists?(:name => name) } end end class project < ActiveRecord::Base belongs_to :student has_one :teacher end class teacher < ActiveRecord::Base belongs_to :project end This does...

Override same Class method in Ruby with Multiple Modules, with need to call super. Do I use Method Alias, or some other clever trick?

Here's the situation: I have a User model, and two modules for authentication: Oauth and Openid. Both of them override ActiveRecord#save, and have a fair share of implementation logic. Given that I can tell when the user is trying to login via Oauth vs. Openid, but that both of them have overridden save, how do "finally" override save...

Ruby on Rails - Primary and Foreign key

Hey, I am creating a site in Ruby on Rails, I have two models a User model and a Transaction model. These models both belong to an account so they both have a field called account_id I am trying to setup a association between them like so: class User < ActiveRecord::Base belongs_to :account has_many :transactions end class Trans...

mysql to active record

hi! having the following models, Contact has_many :group_contact_classifications has_many :groups, :through => :group_contact_classifications GroupContactClassification belongs_to :group belongs_to :contact Group has_many :group_contact_classifications has_many :contacts, :through => :group_contact_classifications i just want to con...

Eager load this rails association

Hi, I have rails app which has a list of users. I have different relations between users, for example worked with, friend, preferred. When listing the users i have to decide if the current user can add a specific user to his friends. -if current_user.can_request_friendship_with(user) =add_to_friends(user) -else =re...

Ruby on Rails Join Table Associations

Hey, I have a Ruby on Rails application setup like so: User Model has_and_belongs_to_many :roles Role Model has_many :transactions has_and_belongs_to_many :users Transaction Model belongs_to :role This means that a join table is used called roles_users and it also means that a user can only see the transactions that have been ...

How Do I Create an 'OR' Condition Using the ActiveRecord Model

Given the following code which creates an and condition, how do I make it create an or condition instead? Country.first(:conditions=>{:media_code=>country_code, :code=>country_code}) ...

Active Record like functionality on array instance variable

I would like to write a module that provides active record like functionality on an array instance variable. Examples of its use would be x = Container.new x.include(ContainerModule) x.elements << Element.new x.elements.find id module ContainerModule def initialize(*args) @elements = [] class << @elements def <<(e...

Storing unicode strings to SQL Server via ActiveRecord

I am using Castle ActiveRecord as my ORM. When I try to store unicode strings, I get question marks instead. Saving unicode strings worked perfectly when I was using mysql, but when I recently switch to SQL Server it broke. How should I go about fixing this? ...

Is there a way to have three way habtm associations in rails / activerecord?

Often three (or more) way associations are needed for habtm associations. For instance a permission model with roles. A particular area of functionality has many users which can access many areas. The permissions on the area are setup via roles (habtm) The user/roles association is also habtm The permissions (read, write, delete, etc...

rails xml to active record object

I've been googling for a while to try and convert and incoming XML request into an active record object. I've tried using the ActiveRecordObject.new.from_xml method but it doesn't seem to handle relationships. For example, say I have the following xml: <blog> <title></title> <blog-pages> <blog-page> <page-number></page-nu...

what is a RoR best practice? match by id or different column?

I had a terrible morning. Lots of emails floating around about why things don't work. Upon investigating I found that there is a data mismatch which is causing errors. Scenario Customer and Address are two tables. Customer contains class Customer < ActiveRecord::Base has_one :address, :foreign_key => "id" end Address...

To Interface or Not?: Creating a polymorphic model relationship in Ruby on Rails dynamically..

Please bear with me for a moment as I try to explain exactly what I would like to achieve. In my Ruby on Rails application I have a model called Page. It represents a web page. I would like to enable the user to arbitrarily attach components to the page. Some examples of "components" would be Picture, PictureCollection, Video, VideoC...