Active Record Associations


#1

Hi,

I am trying to create 3 tables and link them to a fourth. I want table
Post_Type to be linked to tables: Supplies,Textbooks,Miscs. Below are
my table defs and models and controller. I only included a few because
they are similar. I would like be able to display all
supplies,textbooks,and miscs where post_type_id is equal to item_type.
I am very new to ruby and do not think i am going about this correctly.

create_table :post_types do |t|
t.column :Item_Type, :string

create_table :miscs do |t|
t.column :post_type_id, :string
t.column :Dept, :string
t.column :Class, :string
t.column :Qty, :integer
t.column :Price, :float
t.column :Descr, :text
t.column :Email, :string

create_table :supplies do |t|
t.column :post_type_id, :string
t.column :Dept, :string
t.column :Class, :string
t.column :Qty, :integer
t.column :Price, :float
t.column :Descr, :text
t.column :Email, :string

class Post_Type < ActiveRecord::Base
has_many :textbooks, :foreign_key => ‘post_type_id’
has_many :supplies, :foreign_key => ‘post_type_id’
has_many :miscs, :foreign_key => ‘post_type_id’
end

class Misc < ActiveRecord::Base

belongs_to :post_type,
:foreign_key=> ‘item_type’
end

Controller:::::
def test
@post_type = Post_Type.find(:all)
end

I have a rhtml file that displays the information below. It gives me an
error that qty is not defined.

TEXTBOOKS
<% @post_type.each do |i| %> <% end %>
<%= i.miscs.qty %>

Please help. thanks.


#2

On Jun 1, 4:20 pm, “Luke Mr.” removed_email_address@domain.invalid
wrote:

You’ve declared that post_type has_many miscs, so i.miscs is an array,
not a single object.

You either want has_one instead of has_many, or you are wanting to
loop through the i.miscs array to sum up the qty? It’s hard to tell
from your example.

Jeff
softiesonrails.com


#3

Hi,

I think the problem you’re having here is that you’re calling ‘qty’
when your table column is ‘Qty’.

There are also a number of things that are not set up in a way that
will give you the benefit from Rails:

It’s generally standard to keep all of your column names lowercase.
If you can, it’s also best to be as descriptive as possible. For
example, ‘description’ is better than ‘Desc’.

Your Post_Type class would be better named without the underscore:
PostType. If you do this, you shouldn’t need to specify the foriegn
key as you have in your relationship declarations.

For readability, I would also be more descriptive in your controllers
and views:

def index
@post_types = PostType.find(:all)
end

<% @post_types.each do |post_type| %>

In terms of your models, I don’t know exactly what you application is
but I would say your method would work. However, I would suggest
looking at ‘tagging’ and ‘polymorphic’ relationships in Rails as you
may be able to achieve a neater, more managable solution.

Steve


#4

Also consider what Jeff has said :0)

(didn’t spot that during my waffling!)

Steve


#5

Your namings completely messed up. Conventions is really important in
Rails, it’s just not supposed to work that way. Table names should be
in lower case separated by dash, while model name is camel case. like
post_types/PostTypes, text_books/TextBooks etc.
Just learn more about Rails.
On Jun 6, 2:29 am, “Luke Mr.” removed_email_address@domain.invalid


#6

liquidautumn wrote:

Your namings completely messed up. Conventions is really important in
Rails, it’s just not supposed to work that way. Table names should be
in lower case separated by dash, while model name is camel case. like
post_types/PostTypes, text_books/TextBooks etc.
Just learn more about Rails.
On Jun 6, 2:29 am, “Luke Mr.” removed_email_address@domain.invalid

not helpful.


#7

Stephen B. wrote:

Also consider what Jeff has said :0)

(didn’t spot that during my waffling!)

Steve

Thanks guys. I actually just want a table Post_Types that will be able
to access my other three tables: Miscs, Supplies, and Textbooks from a
search page. The Post_Types table just contains an id field with either
1,2,3. 1 is linked to the Textbooks.post_type_id field, 2 to
Supplies.post_type_id field and 3 to Miscs.post_type_id field. I
changed my Post_Type model to has_one relationships after seeing Jeff’s
posts. With my rhtml file below, i am still getting the following
error:

C:/InstantRails-1.4-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:100:in
`const_missing’: uninitialized constant Textbooks

<.rhtml>
<% @post_type.each do |i| %>


<%= i.textbooks.Qty %>

<% end %>

Am I going about my association correctly? I simply want to scroll
through and display all miscs,supplies,and textbooks, by using
post_types. I plan to use your ideas Stephen for naming conventions and
such but first i would just like to figure out how to display the data
from my tables. Thanks for everyone help.


#8

liquidautumn wrote:

what is the difference between supplies, textbooks and miscs?
seems like you have to rework domain model design as well…

On Jun 6, 8:13 pm, “Luke Mr.” removed_email_address@domain.invalid

I am setting up a site where people can post either supplies, textbooks,
or miscellaneous items to sell. When they post under one of the three
categories, the data is stored in one of the three tables. My problem
comes when someone wants to search for the items in these tables. I
would like to have one table linked to these other three that can access
the data in them. I am used to using sql and access where you can link
tables with joins and then see all rows where foreign keys have the same
value. I would like to do something similiar in this situation.


#9

What you need are two separate tables, items and item_types (supplies,
textbooks, misc are item_types in fact)
Item belongs_to :item type, ItemType has_many :items.
Then all your items, despite of its item type will go to items table.
Department looks like separate entity too. Foreign keys should be
integers, not strings. Allow null, where it will not break things
apart.
I bet there are lots of things to alter, you’d better to learn more on
the topic, at least on domain model design and rails basics.

On Jun 7, 2:01 am, “Luke Mr.” removed_email_address@domain.invalid


#10

what is the difference between supplies, textbooks and miscs?
seems like you have to rework domain model design as well…

On Jun 6, 8:13 pm, “Luke Mr.” removed_email_address@domain.invalid