# application\_ruby database block and attributes

**URL:** <https://discourse.chef.io/t/application-ruby-database-block-and-attributes/4173>\
**Category:** Chef Infra (archive)\
**Created:** [July 10, 2013, 5:19pm UTC](https://discourse.chef.io/t/application-ruby-database-block-and-attributes/4173 "2013-07-10T17:19:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cassiano\_Leal](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/cassiano_leal/32/86_2.png) [@Cassiano\_Leal](https://discourse.chef.io/u/Cassiano_Leal)\
**Post date:** [July 10, 2013, 5:19pm UTC](https://discourse.chef.io/t/application-ruby-database-block-and-attributes/4173/1 "2013-07-10T17:19:22Z")

</div>

Hi,

How do I pass parameters (preferably via attributes) to the database block in the application\_ruby cookbook?

What I want to do is something like:

application “my\_app” do  
…  
rails do  
…  
database do  
username node[:my\_app][:db\_user]  
password node[:my\_app][:db\_password]  
end  
end  
end

It looks like the database block does not have access to the node object.

I have the application resource inside a definition, so I also tried to pass parameters to it, like params[:db\_user], but that does not work either.

How do I set those database parameters dynamically?

Cheers,  
Cassiano Leal

---

<div class="post-metadata">

**Author:** ![coderanger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/coderanger/32/5_2.png) [@coderanger](https://discourse.chef.io/u/coderanger)\
**Post date:** [July 10, 2013, 5:25pm UTC](https://discourse.chef.io/t/application-ruby-database-block-and-attributes/4173/2 "2013-07-10T17:25:36Z")

</div>

On Jul 10, 2013, at 10:19 AM, Cassiano Leal wrote:

> Hi,
> 
> How do I pass parameters (preferably via attributes) to the database block in the application\_ruby cookbook?
> 
> What I want to do is something like:
> 
> application “my\_app” do  
> ...  
> rails do  
> ...  
> database do  
> username node[:my\_app][:db\_user]  
> password node[:my\_app][:db\_password]  
> end  
> end  
> end
> 
> It looks like the database block does not have access to the node object.
> 
> I have the application resource inside a definition, so I also tried to pass parameters to it, like params[:db\_user], but that does not work either.
> 
> How do I set those database parameters dynamically?

Its an issue with Ruby's method lookup order. Outside of that block (I usually just do it at the top of the file) do "blah = node[:my\_app]" and then in the block you can do "username blah[:db\_user]" and whatnot since then it knows that blah is a variable and not a method call ☹ method\_missing, got to love it!

--Noah

---

<div class="post-metadata">

**Author:** ![Cassiano\_Leal](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/cassiano_leal/32/86_2.png) [@Cassiano\_Leal](https://discourse.chef.io/u/Cassiano_Leal)\
**Post date:** [July 10, 2013, 5:49pm UTC](https://discourse.chef.io/t/application-ruby-database-block-and-attributes/4173/3 "2013-07-10T17:49:13Z")

</div>

THANK YOU!

I tried that, and since it worked, I went inside my definition and did

parameters = params

and voilà! parameters[:db][:username] works just as well! 🙂

- cassiano

On Wednesday, July 10, 2013 at 14:25, Noah Kantrowitz wrote:

> On Jul 10, 2013, at 10:19 AM, Cassiano Leal wrote:
> 
> > Hi,
> > 
> > How do I pass parameters (preferably via attributes) to the database block in the application\_ruby cookbook?
> > 
> > What I want to do is something like:
> > 
> > application “my\_app” do  
> > ...  
> > rails do  
> > ...  
> > database do  
> > username node[:my\_app][:db\_user]  
> > password node[:my\_app][:db\_password]  
> > end  
> > end  
> > end
> > 
> > It looks like the database block does not have access to the node object.
> > 
> > I have the application resource inside a definition, so I also tried to pass parameters to it, like params[:db\_user], but that does not work either.
> > 
> > How do I set those database parameters dynamically?
> 
> Its an issue with Ruby's method lookup order. Outside of that block (I usually just do it at the top of the file) do "blah = node[:my\_app]" and then in the block you can do "username blah[:db\_user]" and whatnot since then it knows that blah is a variable and not a method call ☹ method\_missing, got to love it!
> 
> --Noah
