activerecord

duplicate entry validation in rails model

If I have a model Department with columns user_id and group_id When the action tries to save an entry into this model that already exists, i.e. 1 (user_id), 22 (group_id) already exists, at that time I want to raise a violation. What is the way to do this in rails? Following is the code I am using to save right now: if @department.sa...

activerecord: how can I get polymorphic entities with different collections loaded in 1 go?

ok, so i got class A < ActiveRecord::Base has_and_belongs_to_many :cs and class Aa < A end class Ab < A belongs_to :b end How can I get all As(the base class) of a certain c, with the Abs joined to their bs - in one go? Currently, I load a certain c, then go c.as, but I cannot manage to get their bs, too... ...

ActiveRecord has_one relationship alongside a has_many relationship

I'm struggling to model a particular relationship with ActiveRecord. I currently have the following setup and working class Schedule < ActiveRecord::Base has_many :tasks end class Task < ActiveRecord:Base belongs_to :schedule end with a database structure that looks like this: schedules - integer:id tasks - integer:id ...

Can I access information from one associated AR object in another when both are unsaved?

Say I open a Rails (2.3.8) script console and try this: a = Account.new(:first_name) = 'foo' i = a.invoices.build p i.account.first_name Account.rb is a model object and contains: has_many :invoices and Invoice.rb is a model as well containing: belongs_to :account, :validate => true In console line 3 above, i.account is nil. I r...

Rspec Rails mocking associated model not working after find()

I have some nested models that require a bit more than the standard accepts_nested_attributes_for logic. Instead of automatically creating or updating child records based on id key, in this case the child records must already exist and have certain other conditions, or it raises an error. So as part of this, I have a parent model itera...

Default conditions for Rails models

I have a model which has a field called deleted, which is used to mark those deleted items. So normally I would just want to query those having deleted = false items, and in some special cases to list those deleted items for restoring. Is it possible to do that? What I could do now is just using a named scope having :conditions => {:de...

RoR, RSpec - problem with validation testing

Through rspec (I'm using rspec-1.3.0, rspec-rails-1.3.2 gems) generator (ruby script/generate rspec_model suggestion section_id:integer user_id:integer subject:string content:text state:string type:string) I created model and model spec and run rake db:migrate and rake:test:prepare After that I started to work on my model spec: require...

activerecord3 IN statement with array

This must be super basic, but I just can't figure it out. I want to select from a table with lots of tag names all the tags who aren't part of the array 'tagnames'. I tried: Tag.where( "name != ?", tagnames ) gives me "Operand should contain 1 column(s)" Tag.where( "name NOT IN ?", tagnames ) gives me a SQL error I know h...

ActiveRecord talk to two databases?

We've recently revamped a project, and are looking to bring all our old data into the new system. The problem is that the schema is marginally different, so a straight SQL import isn't possible. Due to some denormalization and database changes, we'll need to do some massaging of the data before it's ready for import. I was hoping for ...

How to apply conditions when accessing records using a has_many through relationship in Rails?

Hi, I have the following models: class Campaign < ActiveRecord::Base has_many :campaign_keywords has_many :leads, :through => :campaign_keywords end class CampaignKeyword < ActiveRecord::Base belongs_to :campaign has_many :leads end class Lead < ActiveRecord::Base belongs_to :campaign_keyword end I am trying to buil...

How to order by an attribute of a parent for a polymorphic model in ActiveRecord?

I think I worded that correctly... I have a model called asset which is polymorphic: class Asset < ActiveRecord::Base belongs_to :assetable, :polymorphic => true ... end I have a class level method that acts as a scope: def self.some_scope assets = Asset.joins(:assetable).where('assetable.approved_at IS NOT NULL').order('asse...

How to contribute to Rails?

I ran into a problem with ActiveRecord serialization. Apparently, it has trouble serializing hashes where ranges are keys. I found the Rails Lighthouse ticket (https://rails.lighthouseapp.com/projects/8994/tickets/3067-activerecord-cant-deserialize-hashes-with-range-keys). I've never contributed to an open-source project before, and this...

Any Rails plugin to add comments about each column in ActiveRecord migration files?

I'd like to insert COMMENT, which is part of SQL the command, in my migration files. As far as I know, I can add COMMENT to each table and column. I can't remember a plugin name that lets me to write as follows: t.string :name, :comment => "A user's fullname" t.string :label, :comment => "name of color" t.text :value, ...

Active Record serialized attr losing string encodings (probably YAML issue), workarounds?

I'm using Rails 2.3.8 with Ruby 1.9.1 and I'm having a problem with serialized attributes in active record not preserving string encodings. The underlying problem is probably yaml, but I'm wondering if anyone has any good ideas on how to handle this. The app I'm working on has numerous serialized fields some of which contain deep struct...

Rails ActiveRecord Question

Single Table Inheritance using ActiveRecord. Since we can use @test = Employee.all and find all the employees created. How does rails do this? Since we only use a User Table. How does it know about employees and retrieve only employees? Rails Magic? Explanation anyone? Thank you in advance. Base Class : Person (inherits ActiveRecord) Su...

Getting CodeIgniter Active Record's current SQL code

for example, i want to convert this; $this->db->get('table'); to this; 'SELECT * FROM table' is there any function for this? i searched on the user guide of CI but didnt find any solution. ...

Save after rename_column in Rails migration

I'm trying to work around Oracle's inability to change the type of a column with data in it. I cache the attribute's correct value, set it to nil, rename the column and then attempt to re-set the attribute: class SwitchToForeignKeys < ActiveRecord::Migration def self.up registration_countries = {} Registration.all.each do |r| ...

ActiveRecord OR query

This is an embarrassing noob question, but... How do you do an OR query in Rails 3 ActiveRecord. All the examples I find just have AND queries. Edit: without using a SQL string! ...

How do I set attributes in an ActiveRecord Callback?

Hi folks, this seems like a really simple Question...but behold :) Geek name:string Laser geek_id:integer, power:integer Geek has_one :Laser end Laser belongs_to :Geek end simple enough, right? Now I want to create the laser, after a geek gets created, so the new Geek Model looks like this Geek has_one :laser after_create :cr...

How do I set attributes in an ActiveRecord Object only if it is nil?

Sorry, I think I am a bit stupid today class Mutant < ActiveRecord::Base attr_accessible :style before_create :setup_values private def setup_values style = "hardcore" unless style end end I like to call this stuff in the console like Mutant.create(:style => "rampage") # expected is Mutant.first.style == "rampage" ...