activerecord

how to build on ActiveRecord association before parent object has an id?

Hi, I have a model, Foo, that has_many Bars. It has a virtual attribute, current_baz, that returns the baz attribute of the latest Bar from the association (if one exists). I'd like to define Foo.current_baz= so that one can say my_foo.update_attributes(:current_baz => 7), and in particular, I'd like it to work when a new Foo is created...

How to make an association across 3 tables with activerecord / rails?

I have 3 models defined: Palette (has many swatches) Swatch (has one color) Color (?) How should the tables / associations be defined so that from the Palette object you can collect all the colors, for example: @colors = @palette.swatches.colors (Swatches currently store a color_id, palette_id, plus some related info such as sort_...

How to organize the relationship between two entities

how to organize ORM relationship between the models: Post, Images Post contains Images as many to many. The post contains a previews of the images (cover). How to connect the cover class Post <ActiveRecord:: Base has_many: post_image has_many: images,: through =>: post_image end class Image <ActiveRecord:: Base has_many: post_i...

Stumped by ActiveRecord controller error

I'm making a message board application. Users can make a post, each post requires a tag. Users can comment on the posts. Pretty simple. I've been hacking away on it and got an error I can't explain. I made a post, message#index shows the the list of posts including the newest one. The title of each post links to the message#show view (no...

Weird Rails 3 scope behavior

I've implemented the following scope in a rails 3 application: scope :popular, lambda { |l = 5| order('views desc').limit(l) } However, it seems that when you attempt to count its records directly it doesn't apply the scope filters. For example: Post.popular.size #=> 20 Checking the log, it executes the following query: SQL (0.4m...

Rails activerecord composite key Id and type

Say I have a table animals that has a column parent_id Say also that I have a type_id on the table to indicate species So in my Animal class, Animall.all would return all animals. Say that now I add two new animal types, dog and cat in the animal_types table. So now some animals have type_id of 1 and some 2. I could write a named_sco...

Rails memcached: Where to begin?

I read a few tutorials on getting memcached set up with Rails (2.3.5), and I'm a bit lost. Here's what I need to cache: I have user-specific settings that are stored in the db. The settings are queried in the ApplicationController meaning that a query is running per-request. I understand that Rails has built-in support for SQL cachei...

associations in rails from front end

I have a User and Group model. User has_many Groups and Group belongs_to User I already have entries in the user table: id name -------------- 1 testuser 2 someotheruser Imagine if the user with id 1 (testuser) is logged in and I want to create groups inside that user. When I create a new group from new action in Group C...

Searching by bitmask in ActiveRecord

I have a users table with a bitmask field that has a permissions mask in it. Locally, I can determine whether a user has a certain permission by doing a bitmask (UserPermissions&Perm)==Perm. However, I want to be able to issue a find_by_mask or something similar, perhaps using a :conditions, but I can't seem to find out how I can query t...

How do I specify associations in Rails that pass through several models

I'm writing some tricky polymorphic relationships to handle tagging. I have a Tag model, and a Tagging model which belongs_to a polymorphic taggable. I have an Item model, which has_many :taggings, :as => :taggable, and has_many :tags, :through => :taggings, so that I can call @item.tags. This is all working ok. I want to bring anoth...

method_missing override is not working

I wrote a convenience ActiveRecord extension to delegate methods to a base object (based on multi-table inheritance) class ActiveRecord::Base def self.acts_as(base) class_eval %Q{ def method_missing(method, *args, ActiveRecord::Base belongs_to :my_object acts_as :my_object end # base class class MyObject < ActiveRecor...

Including Rails ActiveRecord methods in Sphinx searches

I'm using Thinking Sphinx to power the search on my Rails application. I know the guide explicitly says that you can't index model methods, but I would like to. Specifically, I have a model whose instances can be tagged through a has_many_through relationship via acts_as_taggable_on_steroids. The important caveat: the model also nests ...

problem: activerecord (rails3), chaining scopes with includes

In Rails3 there seems to be a problem when chaining two scopes (ActiveRelations) that each have a different include: Consider these two scopes, both of which work fine on their own: First scope: scope :global_only, lambda { |user| includes(:country) .where("countries.area_id <> ?", user.area) } Work.global_only(user) => (cut list of...

Why isn't ActiveRecord a module?

To get all the ActiveRecord goodies your class must inherit from ActiveRecord::Base: class Post < ActiveRecord::Base Why not ask the user to include ActiveRecord as a module instead? The advantage of this approach is that Rails could automatically include ActiveRecord in all classes in the models directory, making it completely trans...

updating boolean attributes in postgresql from rails

I have a simple statement like this: @employee.update_attributes(:subscribed=>false) but this is not updating the boolean column field subscribed. It throws a warning saying: WARNING: Can't mass-assign these protected attributes: subscribed ...

Rails 3.0, want to match 'UK' == 'United Kingdom' in ActiveRecord model

hey I have a problem, I have a Country model. where the entry in the DB is: {:name => 'United Kingdom'} The data I have is UK instead of United Kingdom. in the other models where i search for it i have done: country_name = "United Kingdom" if country.name == "UK" but i have to do this all over the application and that is just bad. S...

Complex rails find ordering

Hi, I am trying to do a find which orders results by their house name and then by the customer's last name. Customer.find(:all, :conditions =>['customers.id IN (?)', intersection], :joins => 'JOIN histories ON histories.customer_id = customers.id JOIN houses ON histories.house_id = houses.id', :order => "houses.name ...

Relationship like Twitter followers/followed in ActiveRecord

I'm trying to represent a relationship between users in my application where a user can have many followers and can follow other users. I would like to have something like user.followers() and user.followed_by() Could you please tell me in details how to represent this using ActiveRecord? Thanks. ...

Is it possible to add a function-based database-agnostic index via a Rails/ActiveRecord migration?

I have an ActiveRecord model like this: create_table "books" do |t| t.string "title" end class Book < ActiveRecord::Base default_scope :order => 'lower(title) DESC' end As you can see I want to sort by the lowercase form of the title attribute, but this imposes a performance hit at the database level. This hit can be remedied in ...

build relationship from list of ids

Hi I have has_and_belongs_to_many relationship between Posts and Comments and in my edit action I have form that returns me list of ids 1,3,5,8 etc. I want build relationship between my current model and all models which ids are in the list so @post.comments will return Comments with 1,3,5,8 ids In fact I need execute DELETE FROM comm...