activerecord

ActiveRecord migration change primary key for no reason

I've got a table with five columns, one of which is the primary key. This primary key is using a non-standard name, like nearly all tables in that database. I've added a migration to rename the other four columns in that table. The problem is after running that migration, the primary key constraint is gone! I've done that renaming thing ...

Rails3: combine scope with OR

Hi, I need to combine name scope with or operator... Something like: class Product < ActiveRecord::Base belongs_to :client scope :name_a, where("products.name = 'a'") scope :client_b, joins(:client).where("clients.name = 'b'") scope :name_a_or_b, name_a.or(client_b) end Thx ...

Read database.yml from inside ActiveRecord migration

How do I access the contents of database.yml inside an ActiveRecord migration? Specifically, I need the value of the database key for the database being migrated. ...

How to get primary key after calling ActiveRecord save() method PRADO?

Hi, I am starting out with PRADO php framework and i have a quick question: How to get the Primary Key of the last inserted record? My codes are as follow: $studentRecord = new StudentRecord; $studentRecord->Name = $this->txtName->SafeText; $studentRecord->ContactNumber = $this->txtContactNo->SafeText; $studentRecord->save(); // $stude...

How to convert apache log time to activerecord :datetime

I'd like to save apache logs to MySQL via ActiveRecord. In the apache log, the default time string is like: [13/Aug/2008:00:50:49 -0700] How can I convert it to a ActiveRecord :datetime type? Thanks! ...

Rails Arel selecting distinct columns

I've hit a slight block with the new scope methods (Arel 0.4.0, Rails 3.0.0.rc) Basically I have: A topics model, which has_many :comments, and a comments model (with a topic_id column) which belongs_to :topics. I'm trying to fetch a collection of "Hot Topics", i.e. the topics that were most recently commented on. Current code is as f...

belongs_to has_one structure

I have an application which has the following characteristics There are Clubs Each Club has Teams Each Team has Players I have a users table. The user table basically contains the username and password for the club manager, team manager and the player to login to the system. How should I structure the models and the tables? I plan...

Rails activerecord: query for record by a datetime field?

I'm trying to query for records in the TimeSlot table by the field start_date which has type datetime. Here's what I've tried so far that's failed: TimeSlot.where(:start_date => DateTime.new(2010, 9, 1)) TimeSlot.where(:start_date => DateTime.new(2010, 9, 1).to_s) TimeSlot.where(:start_date => "2010-09-08") TimeSlot.where(:start_date...

Where does a rails (3) application define what is supposed to happen in activesupport-3.0.0.rc/lib/active_support/testing/setup_and_teardown.rb?

I generated a model file but then chose to let it single table inherit from another model. I had to run no migration, because the columns all were already there. Now whenever I want to run tests, I get complaints that the table for the model does not exist. So I have Product < Article < ActiveRecord::Base, product has no own table (doe...

Serializing a MongoDB grid ID into a string using ActiveRecord

In my Sinatra app, I'm using a MongoDB with Grid to store book covers on Heroku. I want to be able to associate these with the books in my ActiveRecord-driven primary database. Currently, I'm downloading the image from Google Books, storing it in the MongoDB, and storing the BSON::ObjectID object into the database as a string. When I go...

Help with Rails ActiveRecord managing queries

I have 3 models. Users, Groups, Employees all of the three have many to many. user has many groups groups have many users groups have many employees employees have many groups So I've created two new models: Departments (handles many to many between Users and Groups) Employments (handles many to many between Groups and Employees)...

Rails ActiveRecord Model design

I have 3 models. Users, Groups, Employees all of the three have many to many. user has many groups groups have many users groups have many employees employees have many groups So I've created two new models: Departments (handles many to many between Users and Groups) Employments (handles many to many between Groups and Employees)...

Rails: Serialize value as comma-seperated and not as YAML

Hi there, I'm looking for a way to store a serialized value of eg. IDs in a column. In before claims that this is not an optimal design: the column is used for IDs of associated records, but will only be used when displaying the record - so no queries are made with selection on the column and no joins will be made on this column either....

In Ruby on Rails, how to create a record with ID 3 when it was destroyed earlier?

I am trying an experimental Rails project and destroyed record 3 (ID == 3), so now there is record 1, 2, 4, and 5. Is there a way to repopulate this table with record ID from 1 to 5? (this is using mysql) I can use if Item.exists?(i) item = Item.find(i) else item = Item.new end # set some values item.name = "haha" + i item.save...

how to manage 3 many-to-many models in Rails

I'm following Railscast Advice of making a different model to maintain many-to-many relationships. However, I am having trouble extracting transitive relation data. Imagine that there are 3 models with many-to-many: User <-> Color <-> Shades I've made 2 more models: ColorLiking (maintains User <-> Color), DiffShades (maintains Col...

Rails 3 ActiveModel::Serializers seem to need lots of support methods

I'm returning to RoR after not using it for a few years and I'm trying to use ActiveModel to serialise a plain object to XML. I'm doing the following, as per the comments in activemodel/lib/activemodel/serialization.rb: class XmlError include ActiveModel::Serializers::Xml attr_accessor :code attr_accessor :description def at...

How can I find records from today, yesterday and so on with Ruby on Rails?

Hi, I want to find all records, say Posts, created today with Ruby on Rails, then all Posts created yesterday, and so on… how should I do? Thank you, Kevin ...

Rails find conditions... where attribute is not a database column

I think it's safe to say everyone loves doing something like this in Rails: Product.find(:all, :conditions => {:featured => true}) This will return all products where the attribute "featured" (which is a database column) is true. But let's say I have a method on Product like this: def display_ready? (self.photos.length > 0) && (...

Adding an rails activerecord association within a loop

I want to add a has_many through association to a activerecord model class for each symbol in an array. for example PeopleOrganisation::ROLES.each do |role| has_many role.to_s.pluralize.to_sym, :through => :people_organisations, :source => :person, :conditions => "people_organisations.role = '#{role.to_s}'" do def << (ob...

Best Practice to abstract ActiveRecord model queries in Rails?

I'd like to extract out logic from the controllers to somewhere that it can be more DRY. What's the best way of handling something like the following in Rails? For instance, as opposed to having the query in the controllers, I could place it in the model: class SmoothieBlender < ActiveRecord::Base belongs_to :user def self.get_...