Hi radhames, thanks for the feedback. Given that I'm new and the above is not correct, would it be possible to see what you suggest for:
class Instance < ActiveRecord::Base has_many :users has_many :books end class User < ActiveRecord::Base belongs_to :instance has_many :books, :order => "created_at DESC" has_many :instance_books, :through => :instance, :source => :books, :order => "created_at DESC" . . end class Book < ActiveRecord::Base belongs_to :user belongs_to :instance end The relationship goes like this: Instance > User > Book Instance is a collection of user's from a company, based on their domain name example (abc.com) The idea is for only abc.com user's to see their books, which is why the books table has a instance_id value Recommendations? on the above? On Sep 8, 8:04 am, radhames brito <rbri...@gmail.com> wrote: > you are using has many throug the wrong way , that is used for many to many > associations and i think you only have one to many in every model > > and only the models that have belongs_to should have a foreing key > > > > On Wed, Sep 8, 2010 at 11:00 AM, radhames brito <rbri...@gmail.com> wrote: > > create takes a hash and params[:book] is a hash then you are adding a > > second hash with :intance => current_user.intance that is why is says you > > are passing 2 arguments > > > a good idea try doing this > > > @book = current_user.book.new(params[:book]) > > @book = current_user.instance > > if @book.save > > blah blah blah .... > > > On Wed, Sep 8, 2010 at 10:39 AM, Adam <asteg...@gmail.com> wrote: > > >> On Wed, Sep 8, 2010 at 12:55 AM, nobosh <bhellm...@gmail.com> wrote: > > >>> I'm trying to work out your suggestion on how to not have a > >>> instance_id in books. can you take a look at let me know what's wrong > >>> and if this is what you suggest? thxs! > > >>> class Instance < ActiveRecord::Base > >>> has_many :users > >>> has_many :books > >>> end > > >>> class User < ActiveRecord::Base > >>> belongs_to :instance > >>> has_many :books, :order => "created_at DESC" > >>> has_many :instance_books, :through => :instance, :source > >>> => :books, > >>> :order => "created_at > >>> DESC" > >>> . > >>> . > >>> end > > >>> class Note < ActiveRecord::Base > >>> belongs_to :user > >>> belongs_to :instance > >>> end > > >>> .... Then to actually get all the notes for instance_id = 1 > > >>> class NotesController < ApplicationController > >>> def index > >>> �...@books = current_user.instance_books > >>> . > >>> end > >>> . > >>> . > >>> end > > >>> current_user.instance_books doesn't return the right results. Ideas? > >>> thxs > > >> I don't think has_many :through will work that way - it needs a real join > >> table as far as I know. One with two belongs_to relationships. If you're > >> wanting to keep instance_id on books, your original code can work, but Book > >> belongs_to Instance, not has_one Instance :through User. If you want to > >> ensure the instance is the same as the user's, you can add a validation. > >> > validate :instance_is_same_as_user_instance > >> > def instance_is_same_as_user_instance > >> > unless self.user and self.instance == self.user.instance > >> > errors.add(:instance, "must be the same as the instance on this > >> Book's User") > >> > end > >> > end > > >> -- > >> 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 rubyonrails-t...@googlegroups.com. > >> To unsubscribe from this group, send email to > >> rubyonrails-talk+unsubscr...@googlegroups.com<rubyonrails-talk%2Bunsubscrib > >> e...@googlegroups.com> > >> . > >> For more options, visit this group at > >>http://groups.google.com/group/rubyonrails-talk?hl=en. -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.