activerecord

.finding on a method in ActiveRecord

I'm using ActiveRecord and Ruby (outside of Rails) to track some statistics. I added a method (total_cost) to one of my models to do calculations using a couple of the columns in the current model as well as a column from another model. I'd really like to be able to use some of ActiveRecord's provisions for math (averaging, sums) and ...

Creating a named_scope for "no children" in a has_many association

I would like to have a named_scope for blogs with zero posts. The following does not work. class Post < ActiveRecord::Base belongs_to :blog end class Blog < ActiveRecord::Base has_many :posts named_scope :has_no_posts, :conditions => "blogs.id NOT IN (SELECT blog_id FROM posts)" end ...

Separate date and time form fields in Rails

I have an ActiveRecord model Eventwith a datetime column starts_at. I would like to present a form, where date and time for starts_at are chosen separately (e.g. "23-10-2010" for date and "18:00" for time). These fields should be backed by the single column starts_at, and validations should preferably be against starts_at, too. I can of...

Multi value attributes in Rails ActiveRecord model?

I am having a Property model that should contain multiple values (just Strings). With Rails/ActiveRecord it seems that I have to create a new model (and a new table) for those values (like PropertyValue). As every one of those values just stores one String the PropertyValue only need one attribute (like value). I don't like that idea cau...

In Rails models, can I change belongs_to to has_one and has_one to belongs_to?

Hi, If I have two models. Model1 belongs_to Model2, and Model2 has_one Model1. Thus I can access Model2 from Model1 as well as Model1 from Model2. Now my question is, can I change the relationship to Model2 belongs_to Model1 and Model1 has_one Model2? It also can let me traverse from Model1 to Model2 and from Model2 to Model1. I'm not s...

is there a nicer/rails-like way for find all where something IS NOT NULL query?

is there a nicer/rails-like way for this IS NOT NULL query? MyModel.find(:all, :conditions=>"some_reference_id IS NOT NULL") ...

Rails: How to chain scope queries with OR instead of AND ?

I'm using Rails3, ActiveRecord Just wondering how can I chain the scopes with OR statements rather than AND. e.g. Person.where(:name => "John").where(:lastname => "Smith") That normally returns name = 'John' AND lastname = 'Smith', but I'd like: name = 'John' OR lastname = 'Smith' ...

How do I exclude a model from a transaction in ActiveRecord?

I have a special case Model which must not become part of an outer transaction: Outer.Transaction do ... Inner.create(:blah) ... end How do I stop Inner becoming part of the transaction, assuming Inner should know nothing about which specific transaction it is getting pulled into? For example, creating an inner transaction is...

ruby on rails sql statement error

Hi guys i have a model livestream which has two columns in the database, 'user_id' and 'activity_id'. Very straight forward. Now when I try to insert multiple records into the database I get an invalid SQL statement: SQLite3::SQLException: near ",": syntax error: INSERT INTO livestreams (user_id, activity_id) VALUES (1, 2), ...

Rails 3, Modeling Question

Hello, rails 3 newbie, using Devise for auth... I want to create the following models: class Instance < ActiveRecord::Base has_many :users has_many :notes end class User < ActiveRecord::Base belongs_to :instance end class Note < ActiveRecord::Base belongs_to :instance end To create a new note in the notes_controller.rb def cr...

Adding an associated Table to an Existing Table (Book > Revision)

Hello, newbie here... I have the following models: class Instance < ActiveRecord::Base has_many :users has_many :books end class User < ActiveRecord::Base belongs_to :instance end class Book < ActiveRecord::Base belongs_to :instance end I now want to add a BookRevision table, that has the following columns (id, user_i...

Why isn't Rails automatically creating join table entries?

I have a simple has_many through relationship set up: class Tag < ActiveRecord::Base has_many :profile_tags has_many :profiles, :through => :profile_tags end class ProfileTags < ActiveRecord::Base belongs_to :profile belongs_to :tag end class Profile < ActiveRecord::Base has_many :profile_tags has_many :tags, :through => :...

Relational database structure for Texas Hold'em poker data

Hello, I am planning a relational database to store poker game data (like what would be included in the hand histories). I would like help figuring out how to design the associations. It seems like there should be 4 models: Game, Hand, Player, and Action (single action of a given player, like raise, fold, call). Let me lay out what I h...

How do I find records matching attribute with 1+ values in ActiveRecord/SQL?

How do I find records matching attribute with 1+ values in ActiveRecord/SQL? Examples would be something like: Post.find_by_type("Post and/or ChildPost") Post.find(:type => "Post and/or ChildPost") How can I do this? The number of values will be no more than 10 I'd say. ...

Creating a Scope that joins user_id with the User Table (user.id) --- Rails 3

Hello, I'm using Rails 3 with devise. I have a table books which has a column for user_id, which is related to the Users table (user.rb) I'm trying to create a Scope that shows all the books with the user's email address (Books joined to Users) class Note < ActiveRecord::Base scope :personal, proc {|user| where(:user_id => user.id) }...

Rails ActiveRecord: HABTM find parameters

I have 2 models: notes and tags. class Note < ActiveRecord::Base has_and_belongs_to_many :tags end class Tag < ActiveRecord::Base has_and_belongs_to_many :notes end A tag has a name (eg. "rss", "javascript" etc.). What is the best way to retrieve all notes that have a certain list of tags? That is, I would like to have a named ro...

SQLException after an Array.exists?(object) with Rails

Hi, I have this strange SQL error when I try to know wether an object is in an array: @announcement = Announcement.first puts "YAY" if current_user.announcements_read.include?(@announcement) Error: ActiveRecord::StatementInvalid (SQLite3::SQLException: ambiguous column name: created_at: SELECT "announcements".id FROM "announcemen...

Pros/Cons of storing serialized hash vs. key/value database object in ActiveRecord?

If I have several objects that each have basically a Profile, what I'm using to store random attributes, what are the pros and cons of: Storing a serialized hash in a column for a record, vs. Storing a bunch of key/value objects that belong_to the main object. Code Say you have STI records like these: class Building < ActiveRecord:...

How to remove private fields from ADODB ActiveRecordSet

An ADODB ActiveRecordSet contains many fields like _table, _original, _where, etc, which are not usefull to the client interface. To save time and code I often send a JSON-encoded version of the ActiveRecordSet object. An ActiveRecordSet object may contain child tables which also contain those private fields. I cannot use array_walk_recu...

Rails: Query to get recent items based on the timestamp of a polymorphic association

I have the usual polymorphic associations for comments: class Book < ActiveRecord::Base has_many :comments, :as => :commentable end class Article < ActiveRecord::Base has_many :comments, :as => :commentable end class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end I'd like to be able to define ...