activerecord

RoR: Should I use belongs_to, :polymorphic in this scenario?

I am working on a project where many ActiveRecord models can have a conversation associated with it. Users can discuss just about every aspect of the site. I have two ideas as to how this should be implemented. 1) Use a belongs_to in the asset, not the conversation - conversation will be totally unaware of its asset class Product< Acti...

using end as column name

I'm maintaining a rails 2.1 application that has some unfortunate choices for column names. For instance, an Event has a start and an end date. Instead of using start_at and end_at the original design uses start and end. Of course this leads to def end read_attribute(:end) || 1.hour.from_now end I'm surprised this even parses...

acts_as_taggable_on: query for users tagged with any of these tags

I'm using acts_as_taggable_on and I'm trying to query for all users tagged with any of the tags in the collection tags Right now I'm doing: tags.map(&:name).each { |name| @result.push User.tagged_with(name) } Is there a way that I can do this in one query, and not tags.size queries? I'd appreciate any help. ...

Adding a Rating to each User per category on Ruby on Rails

Right now I'm building a social media app, where i want an user to have a rating per category, how would the association go? The way it needs to be setup it's Each user will have a different rating in each category. I'm think that belongs_to :user belongs_to :category in the UserCategoryRating model. and has_many :user_category_r...

'Splitting' ActiveRecord collection

Let's say I have two models Post and Category: class Post < ActiveRecord::Base belongs_to :category end class Category < ActiveRecord::Base has_many :posts end Is there a method that will allow me to do something like posts = Post.find(:all) p = Array.new p[1] = posts.with_category_id(1) p[2] = posts.with_category_id(2) p[3] =...

Changing type of ActiveRecord Class in Rails with Single Table Inheritance

I have two types of classes: BaseUser < ActiveRecord::Base and User < BaseUser which acts_as_authentic using Authlogic's authentication system. This inheritance is implemented using Single Table Inheritance If a new user registers, I register him as a User. However, if I already have a BaseUser with the same email, I'd like to ...

Rails ActiveRecord associations

I have an User model. How I can get all associations between the User model with other models ? I need to know the model names and the association type (1..1, 1..m, m..m ...) I don't want to use the db/schema.rb file ...

In Rails, how to get the actual Query when an ActiveRecord query executes

Hi, I am using a simple query in ActiveRecord which does something like this. MyTable.find(:all, :conditions => {:start_date => format_time(params[:date]) }) I want to get the equivalent query that is executed in the background, perhaps using a puts statement or something similar to that. MySQL is my database. ...

ActiveRecord / MySQL Select Condition Comparing String Components

I have a string that is defined as one or more dot-separated integers like 12345, 543.21, 109.87.654, etc. I'm storing values in a MySQL database and then need to find the rows that compare with a provided value. What I want is to select rows by comparing each component of the string against the corresponding component of the input strin...

Geocoder Rails plugin - Near search problem with ActiveRecord

This is dragging at my nerves! Scenario: I am using the geocoder plugin: http://github.com/alexreisner/geocoder/ I have two models: Address and Item the latitude and the longitude are in Address and an Item belongs_to :address Now, when I try to do a near search: Item.near(Person.first.address.coordinates, 20) on an Item it fails with...

Recommend a lightweight ORM / Active Record Library

I do not mind which pattern is used. I am just looking for something that is: 1) Lightweight 2) Under active development 3) Well documented 4) Supports MySQL Can anyone recommend anything? ...

Rails/ActiveRecord: has_one, belongs_to and before_create

I'm having trouble figuring out how best to model my data. I have the following two models in my Rails application: class Foo < ActiveRecord::Base belongs_to :active_bar, :class_name => 'Bar' accepts_nested_attributes_for :active_bar before_create do |f| f.active_bar.foo = f # Causes stack overflow! f.active_bar.save...

Problems using ActiveRecord model on existing table.

I've created a model for an existing table using the following generator command: script/generate model Group The table in question have a different name, so I've changed the model to account for it. set_table_name 'demandegroupe' Then I've fired up the console to look if everything was working. >> Group.all [#<Group login: "XXXXX...

Why am I getting a "SystemStackError: stack level too deep" in my rails3 beta4 model

Hi there. I'm getting the following error: SystemStackError: stack level too deep when executing the following code in rails3 beta4 under ruby 1.9.2-rc1: ruby-1.9.2-rc1 > f = Forum.all.first => #<Forum id: 1, title: "Forum 1", description: "Description 1", content: "Content 1", parent_id: nil, user_id: 1, forum_type: "forum", create...

ActiveRecord and has_many, :through link backs

Is there a simple way to specify a has_many :through relationship where you're linking the same data type? eg. One User has many friends (who are all users). I'd like to be able to store data about the friendship, so has_many :through seems to be the obvious choice, but then you'd have to define two :user_id columns, which of course does...

Rails not picking up model attribute despite column EXISTING in table

== AddIpToTutorials: migrating =============================================== -- add_column(:tutorials, :ip_address, :string) -> 0.0022s == AddIpToTutorials: migrated (0.0022s) ====================================== >> t = Tutorial.new .. >> t.ip_address NoMethodError: undefined method `ip_address' for #<Tutorial:0x2b3b35f5a690> ...

validates_one_of

I have a table that contains a column for a boolean value. I want there to be only one row that can have that boolean value be true. Does rails support such a feature? I'm looking for something like the following: Table id | bool_value 1 | false 2 | false 3 | true 4 | false ...

Insert Parent - Children in Castle ActiveRecord

Hi everybody, I am using Castle ActiveRecord to mapping database. I have a simple database like this: And mapping code: Group: [ActiveRecord("[Group]")] public class Group : ActiveRecordBase { private long m_ID; private string m_GroupName; private string m_Description; private IList<Contact> m_...

Generalizing a Fetch() function for data sources

I have a Repository that should adapt itself to any kind of data source. This includes certain types of web services, databases (using ActiveRecord and NHibernate) and even flat files. For ActiveRecord I have EnumerateQuery(IActiveRecordQuery query) which returns an IEnumerable just like I need but the interface is undocumented. What exa...

rails - saving data to another controller

Hi, I am trying to receive data at one controller and then save it on another controller/model From the api I wrote the following: class ExportController < ApplicationController def search @assignment = Assignment.find_by_id(params[:assignment_id]) @assignment.candidates.new(params[:candidate_ids]) @assignment.candidates...