activerecord

Rails: Polymorphic or not?

Considering the following resources with their corresponding relationships: Site has_many :page has_many :groups has_many :projects Group belongs_to :site has_many :pages Project belongs_to :site has_many :pages Is it good to make Page model polymorphic or leave individual foreign keys? **Scenario 1 (Polymorphic)** Page pa...

Dividing results from an activerecord query into two objects

Is there a rails-like way to divide results from an activerecord query? For example, I did @results = Items.find(:all), but I want the top half of items from @results to appear in a line item under <ul class="part1">, and the other half of them to appear under <ul class="part2">. <ul class="part1"> <li><a href="#">result["name"]</a...

Rails: How do you handle dynamic attributes in the database ?

If you come across classifieds websites like craigslist, oodle, kijiji,... everything is a classified, when you are posting a classified, they show different form fields depending upon the category one selects. For example, if you are posting a classified about Car, they show make, model, interiors, etc... How do you think data is handle...

Accounting System design & databases

I am working on a simple invoicing and accounting applications, but I am totally lost on how to represent accounting-transactions(journal-entries) in a database. Using a separate debit and credit column in the database seems like one way, but most of the Open Source Accounting I have seen use a single amount column. This I assume simplif...

How would you represent this sql query in a ActiveRecord relationship?

I have a slightly complicated query that I'd like to have as a natural ActiveRecord relationship. Currently I call @calendar.events and @calendar.shared_events then join the arrays and sort them. The consolidation is able to be done with this sql: SELECT calendar_events.* FROM calendar_events left outer join calendar_events_calendars on...

How do I validate a virtual attribute in an ActiveRecord model?

I am using virtual attributes to save tags in an Entry model from a comma-separated textbox in my form (based on Railscasts #167): class Entry < ActiveRecord::Base has_many :entry_tags has_many :tags, :through => :entry_tags after_save :update_tags attr_writer :tag_names def tag_names tags.map(&:name).join(", ") end ...

SubSonic 3 ActiveRecord lambda expression partially ignored on delete

I have tables Users, Widgets and Layouts. Users have many-to-many relationship with Widgets via Layouts. Each Layout has UserID and WidgetID. I want to delete a Layout that matches specific UserID and WidgetID. Using SubSonic 3 ActiveRecord I write: Layout.Delete(x => x.UserID == user.id && x.WidgetID == id); However, SubSonic delete...

ActiveRecord association getting saved before the record in an update

I have an Entry model which has many Tags. Tags are added to an entry by typing them into a textbox on my form, via a tag_names virtual attribute. Before validation on the Entry model, the tag_names string is converted into actual Tag objects using find_or_create_by_name. The Tag model also has validation to make sure the tag name matche...

has_and_belongs_to_many, avoiding dupes in the join table

I have a pretty simple HABTM set of models class Tag < ActiveRecord::Base has_and_belongs_to_many :posts end class Post < ActiveRecord::Base has_and_belongs_to_many :tags def tags= (tag_list) self.tags.clear tag_list.strip.split(' ').each do self.tags.build(:name => tag) end end end Now...

Rails Active Record: find in conjunction with :order and :group

I have a structure something like this: class User has_many :dongles has_many :licences, :through => :dongles end class Dongle has_many :licences belongs_to :user end class Licence belongs_to :dongle end However, time passes and the user eventually gets multiple licences for each dongle. Reasonably, the app wants to summa...

moderated posts - versioned table

Hi all, I'm creating CMS where created and updated posts have to be moderated. Ie. user Bill updates post - new content is stored somewhere in database. Unless cms-admin accepts Bill's post visitors should see post content before Bill's update. When cms-admin accepts new post content visitors see fresh version. I think about using acts...

What is the default code for bulk has_many :through join assignment in rails?

Hi there, I have a basic has_many :through relationship that is bi-directional: calendars have many calendar_calendar_events calendars have many events through calendar_calendar_events events have many calendar_calendar_events events have many calendars through calendar_calendar_events I'm wanting to assign calendars to an event wit...

Rails: How do I create an instance of a class in a STI hierarchy by it's stored type?

Let's say I am using a STI pattern to persist several different subclasses of "Transaction," which subclasses "ActiveRecord" My subclasses might include "HighPriorityTransaction" and "LowPriorityTransaction" which rails will store in the table "transactions" with a "type" column. Each subclass has a different implementation of the befor...

acts_as_nested_set and inheriting from parent node

I am using awesome_nested_set but I guess this applies to all the nested_set plugins equally. When I add a child I want that child to inherit some attributes from the parent. Sounds simple, it certainly is with acts_as_tree. The problem is you need to save the object before adding to the parent, so at least initially you end up with a r...

ActiveRecord save(false) still validating model

I have a user model on which I check to make sure that the email address supplied is unique: validates_uniqueness_of :email This model acts as paranoid. On destroy, I need to remove the email address so that if the user wants to re-register, they can. For this, I have the following: before_destroy :remove_email def remove_email se...

SubSonic 3 Regenerate ActiveRecord Class Automatically?

I have a SQLite database and SubSonic3, finally got a clue on how to generate the .cs from the .tt in Visual Studio. My stuff builds now. I can kick off MSBuild automatically to build my project, but I would like to add a pre-build event to regen the ActiveRecord.cs cleanly so any database changes end up there for future Unit tests. H...

decorating an attribute in rails

I have a 'name' attribute on a Person model and every time I access the name attribute, I want name.capitalize to be returned. Doing the following inside the model won't work, def name name.capitalize end so what is the alternative? ...

RIA DomainService + ActiveRecord

Hi I tried to use SubSunsonic.ActiveRecord in SL3 project that uses .NET RIA Services. However when I try to return some IQuerable in DomainService class I get an error that the classes generated by Subsonic have a property 'Columns' with an unsupported type. That's what I have public IEnumerable<SE_NorthWind.SuperEmployee> GetInteger...

Sorting ActiveRecord models by sub-model attributes?

Lets assume I have a model Category which has_many Items. Now, I'd like to present a table of Categories sorted on various attributes of Items. For example, have the category with the highest priced item at the top. Or sort the categories based on their best rated item. Or sort the categories based on the most recent item (i.e., the cate...

Writing an ActiveRecord adapter

I'd like to write my own ActiveRecord adapter for the HBase database since none currently exist. However, I've been searching for a while online and can't find any good resources on how to write an ActiveRecord adapter. How would you go about doing this, or are there any links you can recommend? ...