views:

17

answers:

1

how do I change my activerecord model default behavior for the find method? For example, i want to search for all books inside drupal nodes database, but drupal uses only one table for all data, and uses the 'type' column to find out the type

class Book < ActiveRecord::Base
  set_table_name 'node'

  def find(*args)
    :conditions => {:type => 'book'}
    super
  end
end

this is the correct approach to solve this problem?

A: 

I've solved this creating a model for Node and using named_scopes

the result was this

class Node
  set_table_name 'node'
  set_primary_key 'nid'
  named_scope :book, :conditions => {:type => 'book'}

  # if i don't overwrite this method, i would get an error when i try to use the type column
  def self.inheritance_column
    "rails_type"
  end
end

it works, but doesn't look like the rails way of doing stuff. If I get enought time soon, I'll try to write a library to access drupal data using something like acts_as_drupal_node

now i can fetch all book entries using:

@books = Node.book.all
vrsmn