Whats missing on my Ruby 'Inverse Of' relationship
I know this topic has been addressed, but I have been at this for 2 days and I'm just stuck. I know inverse of does not create a new query, so should I use another method?
Question: How to set up an 'inverse of' with a has_one, belongs_to situation & same class..
Explanation: A user 'has_one :spouse' and 'belongs_to :spouse_from'. They are inverse of each other. When a User signs up, they can invite their significant other. For Example
- user_a invites & creates user_b
- user_b.spouse_id is set to user_a.id
- In a separate method I want to be able to update like.. user_a.spouse_id = user_a.spouse.id
The only association that works at this point is user_b.spouse.
Class User
has_one :spouse, class_name: 'User', foreign_key: :spouse_id, dependent: :nullify, inverse_of: :spouse_from
belongs_to :spouse_from, class_name: 'User', foreign_key: :spouse_id, inverse_of: :spouse, optional: true
1 answer
-
answered 2022-05-07 12:04
BenFenner
You need a step 2b in your initial setup to associate a spouse to
user_a
:1a)
user_a
invites & createsuser_b
2a)user_b.spouse_id
is set touser_a.id
2b)user_a.spouse_id
is set touser_b.id
(after savinguser_b
to get an ID for it)
3a) ...This will allow you to later perform the update you wish in another method. The reason that other method update (
user_a.spouse_id = user_a.spouse.id
) doesn't work right now is becauseuser_a.spouse
isnil
at the time since that association hasn't been made yet.However, and maybe most importantly, that update you describe (
user_a.spouse_id = user_a.spouse.id
) will never actually accomplish anything, as the right side will always evaluate to the same value that already exists on the left side.
do you know?
how many words do you know
See also questions close to this topic
-
Why does this Sql query shows the possible changes but does not implement it?
So, I want to change the prefix of my tables and the following command shows the possible changes that will take place which seems alright but does not seem to implement it.
SELECT Concat('RENAME TABLE ', TABLE_NAME, ' TO fan_', SUBSTRING_INDEX(TABLE_NAME, 'pc_',-1), ';') FROM information_schema.tables WHERE table_name like 'pc_%' and table_schema='testdbhere'
Moreover, this isn't a writing privilege issue as changing the tables name individually works perfectly from the same user.
-
how to get weeklytotal and yesterday record in mysql in one table
Hi Everyone i am trying to implement query to get weekly and yesterday data in same table, dummy output i have shared below, if yesterday not exist as per employee_id it should we showing 0 also as per my table week start from monday and end at sunday.please help me out how to query this get weekly_total and yesterday record and one table.
Table name-dailydata-
Sample data
employee_id date total 20 2022-04-25 10 20 2022-04-26 20 20 2022-04-27 20 20 2022-04-28 10 20 2022-04-29 20 20 2022-04-30 30 20 2022-04-31 40 20 2022-05-01 50 40 2022-04-26 20 expected output
employee_id weekly_total yesterday_record 20 200 40 40 20 0 mysql query to get weekly data
select employee_id,sum(total) as week_total from dailydata where date between '2022-04-25' and '2022-05-01'
-
Procedure to display the Employees of a specific Department
Create a stored procedure, to display the lastnames as "Name" from the employee table. The requirement is as below: The name of the Procedure : EmployeesDept
Name of the argument as Input : DeptNo
Write the code to create the procedure
I wrote the Query like this but i didnt get expected output
CREATE PROCEDURE Employeesdept(@Deptno varchar) AS Begin SELECT lastname as name from employee where workdept= 'D21' end
Expected output:
Name --------------- Pulaski Jefferson Marino Smith Johnson Perez Monteverde Name ---------------
-
problems to run rails s on Rails 4 :S
Im trying to run s on my project in Rails 4.2.2
but i get a unexpected error :S
Someone know the reason of the error?
Thanks in advance This is my logs .................................................................................................................................................................................................➜ backend-ror git:(dev) ✗ rails s => Booting Puma => Rails 4.2.2 application starting in development on http://localhost:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server Expected string default value for '--jbuilder'; got true (boolean) Expected string default value for '--serializer'; got true (boolean) Exiting /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:457:in `load': /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/app/controllers/application_controller.rb:39: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ',' or ';' or '\n' (SyntaxError) from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:457:in `block in load_file' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:647:in `new_constants_in' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:456:in `load_file' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:354:in `require_or_load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:494:in `load_missing_constant' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:184:in `const_missing' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/inherited_resources-1.9.0/app/controllers/inherited_resources/base.rb:11:in `<module:InheritedResources>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/inherited_resources-1.9.0/app/controllers/inherited_resources/base.rb:1:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:457:in `load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:457:in `block in load_file' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:647:in `new_constants_in' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:456:in `load_file' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:354:in `require_or_load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:494:in `load_missing_constant' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:184:in `const_missing' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activeadmin-1.4.3/lib/active_admin/base_controller/authorization.rb:2:in `<module:ActiveAdmin>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activeadmin-1.4.3/lib/active_admin/base_controller/authorization.rb:1:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `block in require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activeadmin-1.4.3/lib/active_admin/base_controller.rb:1:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `block in require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activeadmin-1.4.3/lib/active_admin/application.rb:166:in `controllers_for_filters' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activeadmin-1.4.3/lib/active_admin/application.rb:159:in `block (2 levels) in <class:Application>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/config/initializers/active_admin.rb:12:in `block in <top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activeadmin-1.4.3/lib/active_admin.rb:69:in `setup' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/config/initializers/active_admin.rb:1:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `block in load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/engine.rb:652:in `block in load_config_initializer' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/notifications.rb:166:in `instrument' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/engine.rb:651:in `load_config_initializer' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/engine.rb:615:in `each' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/engine.rb:615:in `block in <class:Engine>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/initializable.rb:30:in `instance_exec' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/initializable.rb:30:in `run' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/initializable.rb:55:in `block in run_initializers' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:421:in `block in each_strongly_connected_component_from' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/initializable.rb:44:in `each' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/initializable.rb:44:in `tsort_each_child' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:415:in `call' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:415:in `each_strongly_connected_component_from' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `call' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/initializable.rb:54:in `run_initializers' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/application.rb:352:in `initialize!' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/config/environment.rb:5:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `block in require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/config.ru:3:in `block in <main>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/builder.rb:55:in `instance_eval' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/builder.rb:55:in `initialize' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/config.ru:in `new' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/config.ru:in `<main>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/builder.rb:49:in `eval' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/builder.rb:49:in `new_from_string' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/builder.rb:40:in `parse_file' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/server.rb:300:in `build_app_and_options_from_config' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/server.rb:209:in `app' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/server.rb:61:in `app' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/server.rb:337:in `wrapped_app' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/server.rb:139:in `log_to_stdout' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/server.rb:78:in `start' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:80:in `block in server' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:75:in `tap' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:75:in `server' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:39:in `run_command!' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands.rb:17:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/bin/rails:9:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/bin/rails:9:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/lib/spring/client/rails.rb:28:in `load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/lib/spring/client/rails.rb:28:in `call' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/lib/spring/client/command.rb:7:in `call' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/lib/spring/client.rb:28:in `run' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/bin/spring:49:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/lib/spring/binstub.rb:11:in `load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/lib/spring/binstub.rb:11:in `<top (required)>' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/bin/spring:13:in `<top (required)>' from ./bin/rails:3:in `load' from ./bin/rails:3:in `<main>' ➜ backend-ror git:(dev) ✗
-
Devise Registration controller, every parameter seems to be removed
trying to override Devise Registration controller, following a couple examples online, but for some reason every parameter passed in seems to be removed. this is the controller.
class RegistrationsController < Devise::RegistrationsController skip_before_action :verify_authenticity_token, :only => :create def create p sign_up_params build_resource(sign_up_params) resource.save sign_up(resource_name, resource) if resource.persisted? render json: resource end end
this is the request
curl --location --request POST 'localhost:3000/buyer_users' \ --data-raw '{ "buyer_users": { "email":"peter245@email.com", "password":"123123123", "site_id":"103eb514-8185-4820-8f16-3a9d082c5f60", "account_id": "f362189f-a843-4c19-927a-845ce2cb0ad0" } }'
we seems to be picking up params, but everything is removed
21:16:05 web.1 | Started POST "/buyer_users" for ::1 at 2022-05-06 21:16:05 -0700 21:16:05 web.1 | Processing by RegistrationsController#create as JSON 21:16:05 web.1 | Parameters: {"{\n \"buyer_users\": {\n \"email\":\"peter245@email.com\",\n \"password\":\"123123123\",\n \"site_id\":\"103eb514-8185-4820-8f16-3a9d082c5f60\",\n \"account_id\": \"f362189f-a843-4c19-927a-845ce2cb0ad0\"\n }\n}"=>"[FILTERED]"} 21:16:05 web.1 | {} 21:16:05 web.1 | Completed 200 OK in 7ms (Views: 0.1ms | ActiveRecord: 0.0ms | Allocations: 1155)
routes are set up like this
devise_for :buyer_users, defaults: { format: :json }, controllers: { registrations: 'registrations' }
EDIT: after debugging some more it seems that the entire parameter is taken in as 1 string.
-
Library functions in Inspec Test
Team --
I have a ruby library helper in which I have defined multiple functions/methods. How do I reference those in
Chef
Inspec
tests?def interface_name # some code with logic end
In my specific use case, I am checking to see if a custom networking device was specified as a JSON parameter, and if it was, I am validating it make sure its real (incase they misspelled it) and also gathering its IP and state and other data as reported by
Ohai
This is what I have so far, but I'm not sure if this is correct
describe file('/path/to/custom_attributes.json') do it { should exist } unless json('/path/to/custom_attributes.json').empty? do its(['networking']['interface_name']) { should_not be_empty } interface_name = file(json('/path/to/custom_attributes.json').params['networking']['interface_name']) end end describe file('/etc/NetworkManager/system-connections/wired_connection') do unless interface_name.empty? its('content') { should_not match(/^interface-name=wl.*/mx) } end its('content') { should match(%r{ca-cert=/etc/ssl/certs/ca-certificates\.crt}mx) } its('content') { should match(/id=\.corp.*type=ethernet.*autoconnect-priority=100.*dns-search=corp\.domain.com.*/mx) } end end
The problem / question is that if I gather the parameter directly from the JSON file, then I am bypassing all the validation logic that I'm doing in the library, which defeats the purpose. So, how do I get access to that library function/method in the Inspec test?
For reference, here is the function:
def interface_name file = '/path/to/custom_attributes.json' if File.exist?(file) && !File.stat(file).zero? attributes = JSON.parse(File.read(file)) device_name = attributes['networking']['interface_name'] if device_name && !device_name.empty? && networking_devices.include?(device_name) interface = device_name Chef::Log.info("Valid custom interface provided, using \"#{device_name}\".") else Chef::Log.debug("Invalid interface (\"#{device_name}\") provided. Valid options are: \"#{networking_devices.keys}\"") interface = nil end else Chef::Log.debug('No custom interface provided.') end interface rescue JSON::ParserError nil end
-
Writing structured facts
I've wrote some ruby code which will run on a linux server and return details about the server as a fact. It does this by connecting to amazon and retrieving some json (it runs two separate commands one to retrieve a list of disks - e.g /dev/sda1, /dev/xvdb and then it maps this to a volumeID via another query).
I've made some small amendments to the output and added some values I'm interested in. The code runs multiple times and returns multiple hashes (one for each disk - maybe I should merge them?). Anyway here's an example of a server which has two disks below (this is just some debug output):
Hash is {"/dev/sda1"=>{"disk_mapping"=>"", "is_lvm_partitioned"=>"false", "volumeid"=>"vol1234"}}. Hash is {"/dev/xvdb1"=>{"disk_mapping"=>"xvdb1", "is_lvm_partitioned"=>"true", "volumeid"=>"vol5678"}}.
The next thing I want to to is turn this into a structured fact (with the devices: /dev/sda1, /dev/xvdb1 as the "keys"). Here's a rough idea of how I've done it (I've skipped a lot of the irrelevant code).
json_string = { "#{path}" => { "disk_mapping" => "#{disk_mapping}", "is_lvm_partitioned" => "#{is_lvm_partitioned}", "volumeid" => "#{getVolumes}" } }.to_json hash = JSON.parse(json_string) if hash.is_a? Hash then debug_msg("Hash is #{hash}.") hash["#{path}"].each do |key, child| debug_msg("Setting key: #{key} child: #{child}.") end end
I've never really wrote any ruby before so this is copied from multiple places, but besides aggregated facts I can't find a way to do this; I've tried to do something like this:
Facter.add(:test["#{path}"]["#{key}"]) do setcode do #{child} end end
So I guess in order I want to know:
- Should I merge the hash somehow? I originally assumed I did but found this incredibly hard due to not knowing how many hash's I'd have.
- Should I be using an aggregated fact or a "standard" one?
- How do I retain the structure of the hash and then call it with a single query (e.g facts test).
- Any examples which are similar to my code (the puppet ones I've found quite hard to follow).
Here's what I'm looking for at the end:
[root@blah ~]# facter test { /dev/sda1 => { disk_mapping => "", is_lvm_partitioned => "false", volumeid => "vol1234" }, /dev/xvdb => { disk_mapping => "xvdb1", is_lvm_partitioned => "true", volumeid => "vol5678" } }
Thanks.
-
ERROR: invalid byte sequence for encoding WITH psql
I've seen numerous issues in other posts with the copy command and:
ERROR: invalid byte sequence for encoding "UTF8": 0xfc
And the consensus in these posts appears to be to specify the encoding in the command you're doing the copy with. I have done so:
psql -h localhost -p 5432 -d BOBDB -U BOB -c "\COPY \"BOBTB01\" FROM 'C:\Temp\file.csv' with csv HEADER ENCODING 'WIN1252'"; Password for user BOB: **ERROR: character with byte sequence 0x81 in encoding "WIN1252" has no equivalent in encoding "UTF8" CONTEXT: COPY BOBTB01, line 76589**
So, that confused me and I changed to UTF8 to WIN1252 and having done so I get a slightly different error, the failure is on a different line and the text is slightly different.
psql -h localhost -p 5432 -d BOBDB -U BOB -c "\COPY \"BOBTB01\" FROM 'C:\Temp\file.csv' with csv HEADER ENCODING 'UTF8'"; Password for user BOB: **ERROR: invalid byte sequence for encoding "UTF8": 0xfc CONTEXT: COPY BOBTB01, line 163**
This is the encoding shown in the database:
show client_encoding; client_encoding ----------------- UTF8 (1 row)
The file is from a reliable source and I happen to have "R" installed which also does .csv import. The file was pulled into "R" without issue, that's making me think it's not the file but something else. Is there another switch or syntax that can bypass these issues perhaps?
I'm not sure what is wrong.
Can you help?
Thanks.
-
Normalizing data in postgresql
Flag This application will read an iTunes library in comma-separated-values (CSV) and produce properly normalized tables as specified below. Once you have placed the proper data in the tables, press the button below to check your answer.
We will do some things differently in this assignment. We will not use a separate "raw" table, we will just use ALTER TABLE statements to remove columns after we don't need them (i.e. we converted them into foreign keys).
We will use the same CSV track data as in prior exercises - this time we will build a many-to-many relationship using a junction/through/join table between tracks and artists.
To grade this assignment, the program will run a query like this on your database and look for the data it expects to see:
SELECT track.title, album.title, artist.name FROM track JOIN album ON track.album_id = album.id JOIN tracktoartist ON track.id = tracktoartist.track_id JOIN artist ON tracktoartist.artist_id = artist.id ORDER BY track.title LIMIT 3;
Expected out put is this
The expected result of this query on your database is: title album artist A Boy Named Sue (live) The Legend Of Johnny Cash Jo
DROP TABLE album CASCADE; CREATE TABLE album ( id SERIAL, title VARCHAR(128) UNIQUE, PRIMARY KEY(id) ); DROP TABLE track CASCADE; CREATE TABLE track ( id SERIAL, title TEXT, artist TEXT, album TEXT, album_id INTEGER REFERENCES album(id) ON DELETE CASCADE, count INTEGER, rating INTEGER, len INTEGER, PRIMARY KEY(id) ); DROP TABLE artist CASCADE; CREATE TABLE artist ( id SERIAL, name VARCHAR(128) UNIQUE, PRIMARY KEY(id) ); DROP TABLE tracktoartist CASCADE; CREATE TABLE tracktoartist ( id SERIAL, track VARCHAR(128), track_id INTEGER REFERENCES track(id) ON DELETE CASCADE, artist VARCHAR(128), artist_id INTEGER REFERENCES artist(id) ON DELETE CASCADE, PRIMARY KEY(id) ); \copy track(title,artist,album,count,rating,len) FROM 'library.csv' WITH DELIMITER ',' CSV; INSERT INTO album (title) SELECT DISTINCT album FROM track; UPDATE track SET album_id = (SELECT album.id FROM album WHERE album.title = track.album); INSERT INTO tracktoartist (track, artist) SELECT DISTINCT ... INSERT INTO artist (name) ... UPDATE tracktoartist SET track_id = ... UPDATE tracktoartist SET artist_id = ... -- We are now done with these text fields ALTER TABLE track DROP COLUMN album; ALTER TABLE track ... ALTER TABLE tracktoartist DROP COLUMN track; ALTER TABLE tracktoartist ... SELECT track.title, album.title, artist.name FROM track JOIN album ON track.album_id = album.id JOIN tracktoartist ON track.id = tracktoartist.track_id JOIN artist ON tracktoartist.artist_id = artist.id LIMIT 3;
What am i doing wrong with the code?