activerecord

How can I use locales in a model named_scope?

Hi, I have a named_scope in my model and want a different condition, based on the locale. On the development this code works perfect, after switching to production the named_scope doesn't work and I get every time the first condition ("de"). I think it's a caching problem? I'm working with 2.3.5. and env settings out of the box. named_...

Disable transactions in ActiveRecord

How do I disable transactions in Rails' ActiveRecord? I have a specific situation where I want them to go away I can't seem to find anything useful out there. Is it even possible? ...

Ruby on Rails: how to get error messages from a child resource displayed?

I'm having a difficult time understanding how to get Rails to show an explicit error message for a child resource that is failing validation when I render an XML template. Hypothetically, I have the following classes: class School < ActiveRecord::Base has_many :students validates_associated :students def self.add_student(ba...

Database nesting model layout confusion

I'm no expert in databases and a beginner in Rails, so here goes something which kinda confuses me... Assuming I have three classes as a sample (note that no effort has been made to address any possible Rails reserved words issue in the sample). class File < ActiveRecord::Base has_many :records, :dependent => :destroy accepts_neste...

Finding records that overlap a range in Rails

So, I have an Event model that has a starts_at and a ends_at column and I want to find events that take place in a range of dates. I've come up with this named_scope (range is typically a month): named_scope :in_range, lambda { |range| {:conditions => [ 'starts_at BETWEEN ? AND ? OR ends_at BETWEEN ? AND ?', range.first, rang...

How to mix mongodb and a traditional db in Rails?

I am considering using MongoDB (mongo-mapper) for a portion of my rails application. I am not ready to go whole hog MongoDB because there are too many useful gems that depend on a traditional DB. That being said there are parts of my application that would be great to leverage a document database. Has anyone had success mixing the two...

[Ruby On Rails] belongs_to with :class_name option fails.

I have no idea what went wrong but I can't get belongs_to work with :class_name option. Could somebody enlighten me. Thanks a lot! Here is a snip from my code. class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.text :name end end def self.down drop_table...

Is it possible to specify a SQL Server schema name in a Rails ActiveRecord migration?

The current convention where I work is to use SQL Server schemas like namespaces (e.g. Company.Employees, Company.Branches, etc.) Is it possible to get an ActiveRecord migration to use anything other than the default "dbo" schema in SQL Server? ...

Ruby on Rails has_one Model Not Supplying ID Column

I have a legacy rails (version 1.2.3) app which runs without issue on a number of servers (not to mention my local environment). Deployed to its newest server, though, and I now get ActiveRecord::StatementInvalid: Mysql::Error: #23000Column 'video_id' cannot be null errors. Below are the models/relationships, simplified: class Video < ...

Validating and ActiveRecord model based on an associated Model's data?

Hi everyone. SCENARIO: Given that a model called Edition has its community feature enabled I want all Records under that Edition to validate for the community field When the community feature is disabled, the community field will NOT be validated Basically, I am trying to write a custom validation function at the ActiveRecord level...

Ruby: Dynamically calling available methods raising undefined method (metaprogramming)

I have an Activerecord object called Foo: Foo.attribute_names.each do |attribute| puts Foo.find(:all)[0].method(attribute.to_sym).call end Here I'm calling all attributes on this model (ie, querying for each column value). However, sometimes, I'll get an undefined method error. How can ActiveRecord::Base#attribute_names return an ...

Complicted ActiveRecord Association. Going through a 4th table

I have kind of a complicated case and am wondering how this would work in rails: I want to categories the genres of some singers. Singers can belong to more than one genres, and users can assign tags to each genre For example: singers <-- singers_genres --> genres <-- genres_tags --> tags SQL would look something like: SELECT * FR...

Ruby on Rails bizarre behavior with ActiveRecord error handling

Can anyone explain why this happens? mybox:$ ruby script/console Loading development environment (Rails 2.3.5) >> foo = Foo.new => #<Foo id: nil, customer_id: nil, created_at: nil, updated_at: nil> >> bar = Bar.new => #<Bar id: nil, bundle_id: nil, alias: nil, real: nil, active: true, list_type: 0, body_record_active: false, created_at:...

Building a wiki like data model in rails question.

I have a data model in which I would like to have an item that has a description that can be edited. I would like to also keep track of all edits to the item. I am running into issues with my current strategy, which is: class Item < ActiveRecord::Base has_one :current_edit, :class_name => "Edit", :foreign_key =>...

How to add a has_many association on all models

Right now I have an initializer that does this: ActiveRecord::Base.send :has_many, :notes, :as => :notable ActiveRecord::Base.send :accepts_nested_attributes_for, :notes It builds the association just fine, except when I load a view that uses it, the second load gives me: can't dup NilClass from: /usr/lib/ruby/gems/1.8/gems/activereco...

Rails undo at database level

I'd line to create some kind of undo in my rails app, that's based on an sqlite3 database. For now, a database wide undo would suffice, I mean an undo that can return back in time through states of the whole database, not single tables (for avoiding problems with references etc..) I found vestal_versions, but it's table level and a bit ...

How to coerce type of ActiveRecord attribute returned by :select phrase on joined table?

Having trouble with AR 2.3.5, e.g.: users = User.all( :select => "u.id, c.user_id", :from => "users u, connections c", :conditions => ... ) Returns, e.g.: => [#<User id: 1000>] >> users.first.attributes => {"id"=>1000, "user_id"=>"1000"} Note that AR returns the id of the model searched as numeric but the selected user_id of...

How do I merge a transient entity with a session using Castle ActiveRecordMediator?

I have a Store and a Product entity: public class Store { public Guid Id { get; set; } public int Version { get; set; } public ISet<Product> Products { get; set; } } public class Product { public Guid Id { get; set; } public int Version { get; set; } public Store ParentStore { get; set; } public string Nam...

1 has_many:.., :through and 1 has_and_belongs_to_many: .. problem??

The following are the models and association. class Vendor < ActiveRecord::Base attr_accessible :name, :address_attributes has_many :campaigns has_many :clients, :through => :campaigns end class Client < ActiveRecord::Base attr_accessible :name has_many :campaigns has_many :vendors, :through => :campaigns end class Campaig...

Overriding to_xml for collection of ActiveRecord objects

Okay, I know you can override the to_xml method for a single instance of ActiveRecord object and it works just fine for me. But how would I go about overriding the to_xml method for collection of objects? Suppose for Task model instance, I implemented to_xml which looks like this. def to_xml super(:methods => [:tag_list], :include =>...