Posts Tagged ‘mysql’

  • Associations accross a joining table that uses polymorphic associations in rails 1.2.6

    Date: 2012.03.22 | Responses: 0

    Update: I’m using rails 1.2.6 which does not seem to support :through, :source with has_one associations (the docs don’t show it listed). An alternative is to use “has_many :categories” instead and then create helper method “category” to return a single category.


    Say you have a subjects table that will contain two types of classifications and they are differentiated by a flag, let’s call them subjects(flag off) and categories (flag on).

    Then we have a joining table that will serve as a polymorphic join between all kinds of models and the classifications “subjects” and “categories”. As an example, one of these models will be Resource. “resources” is its respective table.

    subjects
    --------
    ID
    name
    flag
    items_classifications
    --------
    ID
    item_id
    item_type
    classification_id
    resources
    --------
    ID
    name
    ...

    Now I have made the following associations in the models.

    class Classification < ActiveRecord::Base
      set_table_name :subjects
      has_many :items_classifications
    end
    class ItemsClassification < ActiveRecord::Base
      belongs_to :item, :polymorphic => true
      belongs_to :classification
    end

    The Resource model is the tricky one as I only want the resource model to have one “category,” however the console throws up every time I try to instantiate a Resource instance and the problem lies with my “has_one :category” definition. Interestingly, “has_many :subjects” is handled just fine.

    class Resource < ActiveRecord::Base
      has_many :items_classifications, :as => :item,  :dependent => :destroy
      has_many :subjects, :through => :items_classifications, :source => :classification, :conditions => ["subjects.flag IS NULL"], :order => 'subjects.name'
     
      #throws error:
      has_one :category, :through => :items_classifications, :source => :classification, :conditions => ["subjects.flag IS NOT NULL"], :order => 'subjects.name'
    end

    The exact error it throws is the following:

    ArgumentError: Unknown key(s): through, source

    This is strange as the docs say those keys are ok: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_one

    If you have any idea how to make this work or if it’s even possible, let me know!

  • Installing WordPress MU

    Date: 2010.03.19 | Responses: 0

    Always make sure to do the following (and read the README.txt file)

    • “127.0.0.1 localhost.localdomain” to /etc/hosts
    • Activate rewrite apache module.
    • Make sure you have packages php5-mysql, libapache2-mod-php5, libapache2-modxslt
    • Make mysql user and database, for example:
    CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'mypassword';
    GRANT FILE ON *.* TO 'wordpress'@'localhost';
    CREATE DATABASE IF NOT EXISTS wordpressmu;
    GRANT ALL ON wordpressmu.* TO 'wordpress'@'localhost';
  • Mysql: Remove all tables from database

    Date: 2009.05.20 | Responses: 0

    mysqldump -u[USERNAME] -p[PASSWORD] –add-drop-table –no-data [DATABASE] | grep ^DROP | mysql -u[USERNAME] -p[PASSWORD] [DATABASE]

    Source

About me

1

I live in Denver, Colorado and work as a contractor for HAIKU Learning Systems, Inc., an online learning company. I did my Master's in Music Informatics at Indiana University and got a B.A. in Computer Science and Mathematics, with a minor in Music from Carroll College in Helena, Montana.

Resume Contact me

Blog Tags