Tried this too:

@book = current_user.books.create(params[:book].merge(:instance_id =>
current_user.instance_id))

Also ended up inserting a NULL into the DB

On Sep 7, 9:51 pm, nobosh <[email protected]> wrote:
> Just tried:
> params[:book][:instance_id] = current_user.instance_id
> @book = current_user.books.create(params[:book])
>
> for some reason, instance_id is still not being inserted into the DB.
> In the dev log it shows as NULL. Any ideas?
>
> On Sep 7, 9: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...
> > And I like your first suggestion assuming you like the models above:
>
> > > params[:book][:instance_id] = current_user.instance_id
> > > @book = current_user.books.create(params[:book])
>
> > On Sep 7, 9:37 pm, Adam <[email protected]> wrote:
>
> > > On Tue, Sep 7, 2010 at 11:02 PM, nobosh <[email protected]> wrote:
> > > > Hello, Rails newbie....
>
> > > > How can I go From:
>
> > > > @book = current_user.books.build(params[:book])
>
> > > > TO:
>
> > > > @book = current_user.books.create(params[:book], :instance =>
> > > > current_user.instance_id)
>
> > > > right now I get the following error "wrong number of arguments (2 for
> > > > 1)"
>
> > > > Thanks
>
> > > I'm assuming that 'instance_id' is an attribute of a Book. In that case, 
> > > it
> > > needs to be part of the Hash you pass to #build or #create. You can do
> > > either:> params[:book][:instance_id] = current_user.instance_id
> > > > @book = current_user.books.create(params[:book])
> > > or
> > > > @book = current_user.books.create(params[:book].merge(:instance_id =>
>
> > > current_user.instance_id))
> > > Both assume that params[:book] is non-nil and is a Hash. I'd prefer the
> > > first - it's more readable.
>
> > > Adam

-- 
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