What the Ruby Association

Hi everyone,

I just started learning ruby on rails coming from php and mysql worlds.
The tutorial are getting really confusing!!! According to this
http://guides.rubyonrails.org/migrations.html if you want to add foreign
key to a model it is not possible unless you use SQL format and
explicitly tell it where it is.

On the other-side, you have this
http://guides.rubyonrails.org/association_basics.html tutorial it is
possible by simply telling which field are related.

Now which tutorial i am suppose to believe??? Also if the association
together does it return the entire order rows or just one item of it.
and if i look in a db_migrate file why is there no id field already in
the file? Where does it get created?

Thanks but its a bit confusing coming from other language.

Hi everyone,

I just started learning ruby on rails coming from php and mysql worlds.
The tutorial are getting really confusing!!! According to this
http://guides.rubyonrails.org/migrations.html if you want to add foreign
key to a model it is not possible unless you use SQL format and
explicitly tell it where it is.

I think that's referring to actual DB-enforced foreign key constraints. ActiveRecord provides a lot of helpful methods in the associations that basically mimc what a traditional fk would do, but at the Ruby level.

On the other-side, you have this
http://guides.rubyonrails.org/association_basics.html tutorial it is
possible by simply telling which field are related.

I would follow this until you feel some pain that it doesn't solve. If you find that a real fk is the only way, look at the Foreigner gem, which is an ActiveRecord extension that wraps real DB key constraints.

Now which tutorial i am suppose to believe??? Also if the association
together does it return the entire order rows or just one item of it.
and if i look in a db_migrate file why is there no id field already in
the file? Where does it get created?

Read carefully through the associations guide. The ActiveRecord associations are deep and powerful. Also read through the migrations guide. The id, created_at, and updated_at columns are all "assumed" in any migration, unless you add some flags to suppress them when making the migration.

Thanks but its a bit confusing coming from other language.

But so worth it. I just spent all of yesterday untwisting some PHP I wrote in 2004. Fun? No, not really.

Walter

Walter Davis wrote in post #1068445:

Hi everyone,

I just started learning ruby on rails coming from php and mysql worlds.
The tutorial are getting really confusing!!! According to this
http://guides.rubyonrails.org/migrations.html if you want to add foreign
key to a model it is not possible unless you use SQL format and
explicitly tell it where it is.

I think that's referring to actual DB-enforced foreign key constraints.
ActiveRecord provides a lot of helpful methods in the associations that
basically mimc what a traditional fk would do, but at the Ruby level.

On the other-side, you have this
http://guides.rubyonrails.org/association_basics.html tutorial it is
possible by simply telling which field are related.

I would follow this until you feel some pain that it doesn't solve. If
you find that a real fk is the only way, look at the Foreigner gem,
which is an ActiveRecord extension that wraps real DB key constraints.

Now which tutorial i am suppose to believe??? Also if the association
together does it return the entire order rows or just one item of it.
and if i look in a db_migrate file why is there no id field already in
the file? Where does it get created?

Read carefully through the associations guide. The ActiveRecord
associations are deep and powerful. Also read through the migrations
guide. The id, created_at, and updated_at columns are all "assumed" in
any migration, unless you add some flags to suppress them when making
the migration.

Thanks but its a bit confusing coming from other language.

But so worth it. I just spent all of yesterday untwisting some PHP I
wrote in 2004. Fun? No, not really.

Walter

Thanks I appreciate!

I have one last questions!!

Because i started recently and not on a production server for what i am
building. I created rought models with bunch of fields, and did rake
db:migrate. So now they are created but I want to take those field out.
How should I proceed?? I would like to go into the db/folder find the
model modify it to what i needs exactly then was hoping to do rake
db:migrate to refresh with the newest options, this doesnt seem to work.
What would be my other option, and there are over 50 model so i already
try rake db:reset but that didn't do anything other than resetting my
data.

Walter Davis wrote in post #1068445:

Hi everyone,

I just started learning ruby on rails coming from php and mysql worlds.
The tutorial are getting really confusing!!! According to this
http://guides.rubyonrails.org/migrations.html if you want to add foreign
key to a model it is not possible unless you use SQL format and
explicitly tell it where it is.

I think that's referring to actual DB-enforced foreign key constraints.
ActiveRecord provides a lot of helpful methods in the associations that
basically mimc what a traditional fk would do, but at the Ruby level.

On the other-side, you have this
http://guides.rubyonrails.org/association_basics.html tutorial it is
possible by simply telling which field are related.

I would follow this until you feel some pain that it doesn't solve. If
you find that a real fk is the only way, look at the Foreigner gem,
which is an ActiveRecord extension that wraps real DB key constraints.

Now which tutorial i am suppose to believe??? Also if the association
together does it return the entire order rows or just one item of it.
and if i look in a db_migrate file why is there no id field already in
the file? Where does it get created?

Read carefully through the associations guide. The ActiveRecord
associations are deep and powerful. Also read through the migrations
guide. The id, created_at, and updated_at columns are all "assumed" in
any migration, unless you add some flags to suppress them when making
the migration.

Thanks but its a bit confusing coming from other language.

But so worth it. I just spent all of yesterday untwisting some PHP I
wrote in 2004. Fun? No, not really.

Walter

Thanks I appreciate!

I have one last questions!!

Because i started recently and not on a production server for what i am
building. I created rought models with bunch of fields, and did rake
db:migrate. So now they are created but I want to take those field out.
How should I proceed?? I would like to go into the db/folder find the
model modify it to what i needs exactly then was hoping to do rake
db:migrate to refresh with the newest options, this doesnt seem to work.
What would be my other option, and there are over 50 model so i already
try rake db:reset but that didn't do anything other than resetting my
data.

If you read through the migrations guide, you will learn about rake db:rollback, which will step backward through your migrations. You can then hand-edit the migrations, delete the ones that have rolled back and generate new ones, or some combination of those. Just don't remove an already-run migration without rolling it back first.

Walter

Walter Davis wrote in post #1068451:

That can be done but it is often better to write a new migration to
remove the stuff you have decided you did not need after all.
Rollback is fine if you make a migration, run it, but then realise
that it was a bad idea and want to go back to the previous one and
remove the migration. For any more complex situation I would advise
writing a new migration to undo the previous one instead. Otherwise
it is easy to get confused about the state of the database.

Also, as well as looking through the guides I suggest working right
through a tutorial such as railstutorial.org, which is free to use
online, to get the basics of Rails.

Colin

I don't know specifically -- what do the guides say? I've only ever stepped back one or two at a time, made my adjustments, then run rake db:migrate again to roll back up to the current stage.

Walter

Walter Davis wrote in post #1068458:

Jean-Sébastien D. wrote in post #1068459:

Walter Davis wrote in post #1068458:

rake db:rollback YYYYMMDDHHMMSS_model.rb

I don't know specifically -- what do the guides say? I've only ever
stepped back one or two at a time, made my adjustments, then run rake
db:migrate again to roll back up to the current stage.

Walter

Thanks I appreciate, i find it weird that you must make a new migration
everytime you made a mistake, its seem to be a lot of overhead in
compilation time. Maybe something that future rails should invest. Who
knows I just started learning ruby.

Thanks

One more questions..
If i have two tables it should be declared has follow to have my foreign
key
CUSTOMER ORDER
first customer_id
last pay_to
... ...

Correct then in the model i should specify the relation ship correct?

Walter Davis wrote in post #1068458:

rake db:rollback YYYYMMDDHHMMSS_model.rb

I don't know specifically -- what do the guides say? I've only ever
stepped back one or two at a time, made my adjustments, then run rake
db:migrate again to roll back up to the current stage.

Walter

Thanks I appreciate, i find it weird that you must make a new migration
everytime you made a mistake, its seem to be a lot of overhead in
compilation time. Maybe something that future rails should invest. Who
knows I just started learning ruby.

The migrations are typically only run during development, and then you can install from the schema (which maintains a "current state" of the database at all times) when you get to production. Migrations are a great way to build an application organically, because you can roll them back and undo a spike you tried and didn't like. Use them like Git for your database.

Walter

What compilation time? A migration is only loaded and run when you ask for it.

Colin

Jean-Sébastien D. wrote in post #1068459:

Walter Davis wrote in post #1068458:

rake db:rollback YYYYMMDDHHMMSS_model.rb

I don't know specifically -- what do the guides say? I've only ever
stepped back one or two at a time, made my adjustments, then run rake
db:migrate again to roll back up to the current stage.

Walter

Thanks I appreciate, i find it weird that you must make a new migration
everytime you made a mistake, its seem to be a lot of overhead in
compilation time. Maybe something that future rails should invest. Who
knows I just started learning ruby.

Thanks

One more questions..
If i have two tables it should be declared has follow to have my foreign
key
CUSTOMER ORDER
first customer_id
last pay_to
... ...

Correct then in the model i should specify the relation ship correct?

Can you explain the above again, I don't understand what you are
trying to achieve. Also it is best to talk first about models and
relationships, (has_many, belongs_to and so on) then the table
requirements will follow from that.

So first tell us what the models are and what the relationships are.

Colin

Colin Law wrote in post #1068465:

CUSTOMER ORDER
first customer_id
last pay_to
... ...

Correct then in the model i should specify the relation ship correct?

Can you explain the above again, I don't understand what you are
trying to achieve. Also it is best to talk first about models and
relationships, (has_many, belongs_to and so on) then the table
requirements will follow from that.

So first tell us what the models are and what the relationships are.

Colin

Well i am from MySQL world. I am use to a database has follow

CUSTOMER ORDER
customerID orderID
firstName customerID
lastName price
password proceededAt
emails

In MySQL you would have has follow: PK customerID, orderID, FK
order(customerID) to customer(customerID)

In Rails if I understood I create has follow

CUSTOMER ORDER
first_name price
last_name proceed_at
password
emails

Now in models I would have to say

Customer belongs to order
Order has customer

Now my main questions I understand I dont define customer_id and
order_id in customer and order table because its already assume to have
an id key when created. But in ORDER model should I define
customer_id:integer and in model say
ORder customer_id: has customer. To let it know that this field is
actually a foreign key?

Colin Law wrote in post #1068465:

CUSTOMER ORDER
first customer_id
last pay_to
... ...

Correct then in the model i should specify the relation ship correct?

Can you explain the above again, I don't understand what you are
trying to achieve. Also it is best to talk first about models and
relationships, (has_many, belongs_to and so on) then the table
requirements will follow from that.

So first tell us what the models are and what the relationships are.

Colin

Well i am from MySQL world. I am use to a database has follow

CUSTOMER ORDER
customerID orderID
firstName customerID
lastName price
password proceededAt
emails

In MySQL you would have has follow: PK customerID, orderID, FK
order(customerID) to customer(customerID)

In Rails if I understood I create has follow

CUSTOMER ORDER
first_name price
last_name proceed_at
password
emails

Now in models I would have to say

Customer belongs to order
Order has customer

I think that should be customer has_many orders and order belongs_to
customer. The words make some reasonable sense in english.

Now my main questions I understand I dont define customer_id and
order_id in customer and order table because its already assume to have
an id key when created. But in ORDER model should I define
customer_id:integer and in model say
ORder customer_id: has customer. To let it know that this field is
actually a foreign key?

Since order belongs_to customer then the orders table (note the plural
name for the table) should have a customer_id column. You don't need
to tell rails that it is a foreign key, it will know that from the
model relationships. To specify it in the migration for orders you
can say (as is mentioned in the migrations guide).

  t.references :customer
or
  t.integer :customer_id

As I think I said previously if you work right through some tutorials
all this should become clear.

Colin

This is not true. Lets say you have a working system, being used by thousands of users. The database has enough data that a backup/restore takes half a day. Then you need to add a new feature that requires 3 new tables, and new columns on an existing table. How are you supposed to do this without running a migration against the production system?

I suspect Walter meant "during the development phase" rather than "in
development mode". Migrations are applied to the production database
during development.

Colin

Colin Law wrote in post #1068473:

Can you explain the above again, I don't understand what you are

CUSTOMER ORDER

CUSTOMER ORDER
first_name price
last_name proceed_at
password
emails

Now in models I would have to say

Customer belongs to order
Order has customer

I think that should be customer has_many orders and order belongs_to
customer. The words make some reasonable sense in english.

Now my main questions I understand I dont define customer_id and
order_id in customer and order table because its already assume to have
an id key when created. But in ORDER model should I define
customer_id:integer and in model say
ORder customer_id: has customer. To let it know that this field is
actually a foreign key?

Since order belongs_to customer then the orders table (note the plural
name for the table) should have a customer_id column. You don't need
to tell rails that it is a foreign key, it will know that from the
model relationships. To specify it in the migration for orders you
can say (as is mentioned in the migrations guide).

  t.references :customer
or
  t.integer :customer_id

As I think I said previously if you work right through some tutorials
all this should become clear.

Colin

Sorry here a question in term of modeling!! BTW thanks I am learning a
lots with your comments

But I just want to make sure I get the association concept understood
well. I place a question few days ago on this links
http://railsforum.com/viewtopic.php?pid=153916#p153916 but don't seem to
have quick Answer. I just wanna make sure I understand the concept of
association compared to php

It is no good just posting a link to a thread that appears to have
already been answered. If you have a specific still unanswered
question then post it here. However as I have said twice before you
really should work through some tutorials to get the basics rather
than asking such questions here. Once you have done that the answers
will mostly become obvious to you.

Colin

Colin Law wrote in post #1068538:

Here a final questions, not sure if you may know. I installed
activeadmin has a gem and it allow me to manage any customer, interest
and a manager to control when this one is expired or so

It has the following associations

class Client < ActiveRecord::Base
  has_many :music_interest_managers
  has_many :music_interests, through => :music_interest_managers
end
class MusicInterest < ActiveRecord::Base
  has_many :music_interest_managers
  has_many :clients, through => :music_interest_managers
end
class MusicInterestManager < ActiveRecord::Base
  belongs_to :music_interests
  belongs_to :client
end

But when installing a resources for MusicInterestManager I get the
following error!

Showing
/home/jean/.rvm/gems/ruby-1.9.3-p194/gems/activeadmin-0.4.4/app/views/active_admin/resource/index.html.arb
where line #1 raised:

uninitialized constant ActivitiesManager::Customers
Extracted source (around line #1):

1: render renderer_for(:index)
Rails.root: /home/jean/rail/wyw

Application Trace | Framework Trace | Full Trace

Maybe i should ask the activeadmin forum, but i just wanted to see if
you guys knew anything about it!!Thanks!