activerecord

Non-normalized association with legacy tables in Rails and ActiveRecord

I am building a Rails application accessing a legacy system. The data model contains Customers which can have one or more Subscriptions. A Subscription always belong to one and only one Customer. Though not needed, this association is represented through a join table "subscribes", which do not have an id column: Column | Type...

Rails & MySQL - Saved HTML Being Truncated at HTML Entity

I'm saving whole HTML documents in the database with ActiveRecord. I can see in the logs that correct INSERT statements are being generated and sent to MySQL but when I actually look in the database after the save has completed, the content is truncated. It appears that   entities in the document are causing this truncation. I can ...

Joining the same model twice in a clean way, and making the code reusable

I have a model Painting which has a Paintingtitle in each language and a Paintingdescription in each language: class Painting < ActiveRecord::Base has_many :paintingtitles, :dependent => :destroy has_many :paintingdescriptions, :dependent => :destroy end class Paintingtitle < ActiveRecord::Base belongs_to :painting belongs_to...

rails + ActiveRecord: caching all registers of a model

I've got a tiny model (let's call it "Node") that represents a tree-like structure. Each node contains only a name and a reference to its father: class Node < ActiveRecord::Base validates_presence_of :name, :parent_id end The table isn't very big - less than 100 elements. It's updated rarely - in the last 4 months 20 new elements we...

autosave options in Ruby on Rails

Is there a way to turn OFF autosave in Rails? I don't want modifications to an association to automatically save to the database UNTIL I call save on the parent object. some_parent.some_children << child #should not save, just adds to the association! some_parent.save #now parent and children are saved! It this possible or am I ba...

Saving an ActiveRecord non-transactionally.

My application accepts file uploads, with some metadata being stored in the DB, and the file itself on the file system. I am trying to make the metadata visible in the application before the file upload and post-processing are finished, but because saves are transactional, I have had no success. I have tried the callbacks and calling c...

Joining a one-to-many association with a many-to-many association in Rails 3

I have a many-to-many association between a User class and a Table class. Additionally, i have a one-to-many association between the User and the Table (one User ultimately owns the table). I am trying to access all of the tables which the user may access (essentially joining both associations). Additionally, it would be nice to do this...

Ruby on rails - how to retrieve connection strings

Hi Everyone, Are there any ways to retrieve the database connection string where my ruby is connected? what i would like to get is the: 1) Database name where the ruby is connected 2) The username of the SQL Server 3) Password of the SQL Server 4) Server name I want to store it in session variables. (I'am using MS SQL Server.) Pleas...

Two Tables Serving as one Model in Rails

Is is possible in rails to setup on model which is dependant on a join from two tables? This would mean that for the the model record to be found/updated/destroyed there would need to be both records in both database tables linked together in a join. The model would just be all the columns of both tables wrapped together which may then b...

Rails: Link changes one attribute and then goes back

Rails newbie here. I have a list of items which can have a status represented by an integer (right now its just 1=active 0=inactive). What I want is next to each item a link to change the status of that item. So it might look like this: A nice item - Enable Another pretty item - Disable I can't think of how to make the link...

How to populate select box from db query

Hi, I am trying to populate a ruby on rails select box from a database query, the data comes from 3 tables. My query @data = Session.all :include => { :term => :courses } Object !ruby/object:Session attributes: created_at: 2010-06-17 22:12:05 term_id: "15" updated_at: 2010-06-17 22:12:05 id: "3" course_id: "1" attributes_cache: ...

undefined method `reflect_on_association' for Class:Class error when saving

UPDATE Got it to work by using has_and_belongs_to_many Anyone know why this works ? Hi, I get the following error when saving with active record undefined method `reflect_on_association' for Class:Class my relationships look like this : class Contact < ActiveRecord::Base has_many :classes has_many :sessions, :through => :cl...

Eager loading polymorphic associations with different has_one associations in Rails 2

ActiveRecord gives you an interesting error when you try to eager-load a non-existent association. It looks something like this: ActiveRecord::ConfigurationError: Association named 'secondary_complaint' was not found; perhaps you misspelled it? Now why the hell would anybody want to preload a non-existent association? Check this out. ...

Extensible Rails application that connects to several databases

I am implementing a Rails application that needs to aggregate search results from N independent heterogeneous databases. An example use case would be: User queries "xpto" The query is submitted to all the databases registered on the system The results are transformed and combined in a predefined format User gets the results The appli...

Insert Only: ActiveRecord or Queries?

All that I will do is insert records into the database. However, I would definitly like the database-independence that Ruby/ActiveRecord can offer me. What would be the reccommended choice? Using simple insert queries, and rewriting them, and also maintaining my own database class for things as batch insertions; Using the power of Act...

Active Record joins over 3 tables

Hi, I am trying to find all terms and courses that apply to a contact. Here are my models class Register < ActiveRecord::Base belongs_to :session belongs_to :contact end class Session < ActiveRecord::Base belongs_to :term belongs_to :course has_many :registers has_many :contacts, :through => :registers end Here is the find...

CSV to DataMapper Import

This is probably very simple, but I'm quite new to ruby and active record. I've got a CSV dump of a database which I'm trying to import to a database using DataMapper. I'm having trouble understanding which type of relationships I should define in the models so that it matches what is defined CSV. Here's what data I've got from the CS...

How to map table records to Any type using Castle ActiveRecord

I know it's possible to create Any relationships where the related record could be of any type. Is there a way to tell the ActiveRecord the records in a table belong to many different types even when there is no relationships? For example I have a table in which there is a string field that stores the type of each record in table. I'd l...

Passing association parameters on ActiveRecord object creation

In my application I have 2 classes like this: class City < ActiveRecord::Base has_many :events end class Event < ActiveRecord::Base belongs_to :city attr_accessible :title, :city_id end If I create city object: city = City.create!(:name => 'My city') and then pass parameters to create event like this: event = Event.create!...

How to create a model without primary key in rails

I want to create a model 'Relation' which extends ActiveRecord::Base, set it's table name as 'questions_tags', and without primary key. What should I do? class Relation < ActiveRecord::Base set_table_name 'questions_tags' # set table name, right? # how to define 'no-pk'? end UPDATE Hi, guys. I know use 'create_table' can solv...