activerecord

How to find topics with the most flags

Let's say I have a model called Topic. A Topic has_many :topic_flags (a TopicFlag is used to denote content that has been flagged as inappropriate). My question is this: how can I select the Topics with the most flags (limited at an arbitrary amount, descending)? I've thought about it for a while, and while I can hack together an SQL que...

Benefits, etc of using mySQL over SQLite (RoR)

I'm building a web application right now for my company and for whatever reason, can't get mySQL working on my Mac w/ my Ruby install (OSX 10.5). SQLite works fine though, so would it be a problem to use SQLite for now so I can get to work on this and then just change up my database.yml file to point to a mySQL database when I deploy (a...

What are the differences between Active Record and Repository pattern?

It seems to me that the only difference is that Active Record has CRUD methods in data container class, and the Repository pattern uses separate class for data container and CRUD methods, but surely I'm wrong. What are the differences between Active Record and Repository pattern? When should I use which pattern? ...

rails find: using conditions while including same table twice via different named associations

I have posts which are sent by users to other users. There are two models - :post and :user, and :post has the following named associations: belongs_to :from_user, :class_name => "User", :foreign_key => "from_user_id" belongs_to :to_user, :class_name => "User", :foreign_key => "to_user_id" Both :user and :post have "is_public" column...

Inserting time into a time column with Ruby on Rails

Hello, I have run with a problem which i believe is Active Records fault. I am parsing an XML file which contains jobs. This xml file contains nodes which indicate walltime in the time format 00:00:00. I also have a model which will accept these jobs. However, when the time is larger than an actual 24H time, Active record inserts it as N...

How to unassign a value?

I have a table similar to the following id | name | address both name and address are not nullable(this i achieve through attribute setters only and not by default) When i create a new record without any default values what will be stored in the table? If the value is null -> if i store a value (say name="MyName", address="Th...

ActiveRecord serialization problem

I have a db column which is a serialized Hash: class Foo < ActiveRecord::Base serialize :bar end When I store a hash inside of bar that is a few levels deep, the deeper levels do not seem to properly deserialize when I need them. Objects one level deep get deserialzed just fine. However, objects 2 or more levels deep remain YAML cl...

Find or create by field doesnt work

Ive got a model caleld SpacialBody, and I need to seed some records so first off I added this to my seeds.rb [ ["Sol",0,0,0,"standard"] ].each do |body| nb=SpacialBody.find_or_create_by_name(body[0]) nb.name = body[0] nb.x = body[1] nb.y = body[2] nb.type = SpacialBody::Types[body[3]] nb.class = body[4] n...

'rake:test' with Multiple Database configurations in database.yml

I have a rails project that uses multiple legacy databases. I would like to perform tests that verify that the models are linked together correctly. This is being done so that the team can use TeamCity (by JetBrains) to perform automated testing. database.yaml: test: ... database: application_test ... admin: ... <% if RAILS_E...

Ruby on Rails - has one and belongs to many relationship

This question is related to ruby on rails ActiveRecord associations and how to generate those migrations. I'm trying to build a web-app for a documentation/data management system and I have two models - Arg and Descriptor. (The reason for making a descriptor an object rather than an attribute is for multiple Args to share the same descr...

cannot understand rails activerecord typecast reasons

consider that i have a migration as follows create_table :dummies do |t| t.decimal :the_dummy_number end i instantiate like the following dummy = Dummy.new dummy.the_dummy_number = "a string" puts dummy.the_dummy_number the output for the above is 0.0 how did this happen? since i assign a wrong value shouldn't it raise a...

ActiveRecord - replace model validation error with warning

Hi, I want to be able to replace a field error with a warning when saving/updating a model in rails. Basically I want to just write a wrapper around the validation methods that'll generate the error, save the model and perhaps be available in a warnings hash (which works just like the errors hash): class Person < ActiveRecord::Base #...

Is there a clean way for an ActiveRecord model to declare that its rows expire?

We're adding an expires_at column to one of our models. Among other changes, we've got to track down everything with a belongs_to relationship to the model and add the condition that expires_at must be in the future. Is there a way to do this centrally, so that rather than modifying all the other models, we modify the one that expires?...

Complex ActiveRecord association definition

How would you map the dashed association using ActiveRecord? Is there any ActiveRecord-way of doing it? What I want is to get all unique available promotions for a particular item. Here are things I thought but I don't really like: Using custom finder class Item < ActiveRecord::Base has_many :promotions, :finder_sql => "SELECT..FR...

ActiveRecord::Relation join, how to add a column of a join table to the query result with a new name?

To set the stage, I'm using rails 3 and I have these tables and relationships: user has_many lists list has_many tasks task has_many stints I would like to build a query that allows me to select all of the current users stints, and to have the list.id available as an attribute on each stint in the result. I would need to rename list.i...

Codeigniter Active Record inside custom library class

My function breaks at the line where I make an Active Record call, so I must not be calling things right. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Class Stuff { public function specific($passedArray) { echo "Test"; $CI =& get_instance(); $CI->load->database(); ...

insert record if not exists in sql, duplicate column name

i wanted a solution to insert a record if it isn't there so i searched here and found a solution but i have another problem INSERT INTO closed_answers (question_id, subject_id) SELECT * FROM (SELECT 2, 2) AS tmp WHERE NOT EXISTS ( SELECT question_id FROM closed_answers WHERE question_id = 2 AND subject_id = 2 ) LIMIT 1 the output...

activerecord association preventing record from saving

I have a straightforward relation between two models: Asset and Update. both models (minus unrelated methods) here: http://pastie.org/1062627 I ran into a problem where an Asset record will no longer update. For example a simple test: a = Asset.first ; puts a.description; a.description = "new" ; a.save ; puts a.errors ; puts Asset.f...

Rails join tables or HABTM associations

In the application I'm building, I want to have Events. People can then register for the event by creating a team, by directly registering for an event (no team), or by joining an existing team. I originally set it up as Event has_many teams, Team has_many users, user belongs_to Team and Event. However, I foresee problems if a registe...

How do I do this query with ActiveRecord Class from Code Igniter?

I need to increment the number of comments on an Entries Table, the SQL for this would be: UPDATE entries SET comments = comments + 1 WHERE entry_id = 123; And i was wondering how to do this using Active Record Class form Code Igniter. http://codeigniter.com/user_guide/database/active_record.html#update ...