Adam, interesting , what are you thoughts about when I want to return
all books by instance_id.. won't that kill the db looking at the
user's table?

On Sep 7, 9:54 pm, Adam <[email protected]> wrote:
> On Tue, Sep 7, 2010 at 11:41 PM, nobosh <[email protected]> wrote:
> > Adam,
>
> > Thanks for the answer, let me add a little color as to what I'm trying
> > to get working:
>
> > I have the following models: Users (id, name, email, instance_id,
> > etc...) Instances (id, domain name) Books (id, name, user_id,
> > instance_id)
> > In Rails 3, When a new book is created, I need the user_id, and
> > instance_id to be populated based on the current_user.
> > Currently, user_id is being assigned when I create a new book but not
> > instance_id?
>
> > class Instance  < ActiveRecord::Base
> >  has_many :users
> >  has_many :books, :through => :users, :order => "created_at DESC"
> > end
>
> > class User < ActiveRecord::Base
> >  belongs_to :instance
> >  has_many   :books, :order => "created_at DESC"
> > end
>
> > class Book < ActiveRecord::Base
> >  belongs_to :user
> >  has_one    :instance, :through => :user
> > end
>
> > Thoughts on this? I'm a newbie so I'd love to hear if I going down the
> > right track or not...
>
> There's a discrepancy there - Book has_one instance, but it has an
> instance_id. The foreign key goes with a belongs_to relationship.
>
> The Instance is related to the User, so storing it in two places (once on
> the User, once on the Book) can only cause maintenance problems down the
> line. Better to store it one place (like on the User, which you do now) and
> let the Book retrieve its Instance through the User.
>
> In other words, books should not have an instance_id column. It's unused -
> the relationship is achieved through the has_one relationship set up in the
> Book model.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to