Rails Syntax for entering data in sqlite - ruby-on-rails

I have been following this tutorial, http://ruby.railstutorial.org/chapters/modeling-users?version=3.2#top,
and I tried this in the rails console
User.new(name: "Michael Hartl", email: "mhart#example.com")
SyntaxError: compile error
but when I tried this it worked:
User.new(:name=> "Michael Hartl", :email=>"mhart#example.com")
=> #<User id: nil, name: "Michael Hartl", email: "mhart#example.com", created_at: nil, updated_at: nil>
I am using rails 3.2.1, which I guess this is the problem, but why change the syntax to something more complicated like, :<variable>=> instead of <variable>: ??

You didn't type it correctly.
User.new(name: "Michael Hartl", email: "mhart#example.com")

Related

Rails showing all data from database to index page how to stop it?

i am new in rails and i have generated a resource named a company name:string bio:text and ceo:string but my index view showing all the db records but i haven't done any inspet so why it showing all the data in index page?
it showing like this
[#<Company id: 4, name: "google", bio: "ad company", ceo: "sundar pichaif", created_at: "2021-03-15 12:43:18.821528000 +0000", updated_at: "2021-03-15 13:05:49.407986000 +0000">, #<Company id: 5, name: "basecamp", bio: "task management", ceo: "jason fried", created_at: "2021-03-15 13:27:58.781628000 +0000", updated_at: "2021-03-15 13:27:58.781628000 +0000">, #<Company id: 6, name: "Github", bio: "code", ceo: "Chris Wanstrath", created_at: "2021-03-15 13:30:15.510656000 +0000", updated_at: "2021-03-15 13:30:55.654182000 +0000">]
please help me out :(
open your index file where it shows the data from your database, please remove "=" for example: remove this <%= #companies %> and add like this <% #companies %>, the reason I am telling you to remove is that when you use "=" this sign with <%= ..... %>, it will show all the data in this erb brackets, so remove this and your issue will be resolve, Thanks

Rails console output to recreate objects

I accidentally deleted 1000 objects from a database and now trying to recreate these objects. Thankfully I was able to scroll through my console output and find the records. I copy and pasted the console output which is in this (greatly shortened) format:
[#<Assignment id: 276503, school_id: 2091, listing_id: 251572, created_at: "2018-08-30 05:02:36", updated_at: "2018-08-30 05:02:36">, #<Assignment id: 279532, school_id: 1233, listing_id: 252702, created_at: "2018-08-30 06:19:12", updated_at: "2018-08-30 06:19:12">]
#...
I can't get the console to assign this output to a variable so I can figure out how to use this data to recreate the objects in the db:
irb(main):040:0> a = [#<Assignment id: 276503, school_id: 2091, listing_id: 251572, created_at: "2018-08-30 05:02:36", updated_at: "2018-08-30 05:02:36">, #<Assignment id: 279532, school_id: 1233, listing_id: 252702, created_at: "2018-08-30 06:19:12", updated_at: "2018-08-30 06:19:12">]
irb(main):041:1*
Does anyone have ideas how to turn this console output back into objects in my db?
If you have the problems with assigning a huge array to a variable in the console, you can try to use rake task.
array.each do |e|
options = e.split(',')
school_id = options.detect{|i| i.match?(/school_id/)}.split(':').last
listing_id = options.detect{|i| i.match?(/listing_id/)}.split(':').last
Assignment.create(school_id: school_id, listing_id: listing.id)
end
This should work. It is quite consuming, but from another perspective easy and fast.
I would paste that output on sublime or any other text editor and then format
to become something useful...
A bunch of inserts using SQL or just .create() that i would then paste it on the console

Incorrect association in fixtures

I'm following the guide http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures with Rails 4.1.1 and using named fixtures within one another in the https://github.com/codetriage/codetriage project. However when I try to reference one fixture from another it doesn't work:
# fixtures/issues.rb
issue_triage_sandbox_issue:
id: 4
comment_count:
url: https://api.github.com/repos/bemurphy/issue_triage_sandbox/issues/1
last_touched_at: 2012-11-10 22:20:24.000000000 Z
number: 1
created_at: 2012-11-10 23:23:45.281189000 Z
updated_at: 2012-11-10 23:23:45.281189000 Z
repo: issue_triage_sandbox
title: first test issue in sinatra
html_url: https://github.com/sinatra/sinatra/issues/1
state: open
and
# fixtures/users.rb
issue_triage_sandbox:
id: 1
user_name: bemurphy
name: issue_triage_sandbox
full_name: bemurphy/issue_triage_sandbox
language: ruby
created_at: 2012-11-10 21:50:48.351554000 Z
updated_at: 2012-11-10 21:50:48.351554000 Z
issues_count: 1
You can see that the issue should be loading the repo issue_triage_sandbox repo. But in my tests it's not:
issue = issues(:issue_triage_sandbox_issue)
puts issue.repo
# => nil
puts issue.inspect
#<Issue id: 4, comment_count: nil, url: "https://api.github.com/repos/bemurphy/issue_triage...", repo_name: nil, user_name: nil, last_touched_at: "2012-11-10 22:20:24", number: 1, created_at: "2012-11-10 23:23:45", updated_at: "2012-11-10 23:23:45", repo_id: 915227508, title: "first test issue in sinatra", html_url: "https://github.com/sinatra/sinatra/issues/1", state: "open", pr_attached: false>
Any ideas why the issue is being created with a reference to a non-existant repo?
I think it has to be with the id attribute in the repos fixture.
I'm using yml fixtures. I added the issue and the repo to the existing ones in the project.
issues.yml
issue_triage_sandbox:
user_name: bemurphy
name: issue_triage_sandbox
full_name: bemurphy/issue_triage_sandbox
language: ruby
created_at: 2012-11-10 21:50:48.351554000 Z
updated_at: 2012-11-10 21:50:48.351554000 Z
issues_count: 1
repos.yml
issue_triage_sandbox:
user_name: bemurphy
name: issue_triage_sandbox
full_name: bemurphy/issue_triage_sandbox
language: ruby
created_at: 2012-11-10 21:50:48.351554000 Z
updated_at: 2012-11-10 21:50:48.351554000 Z
issues_count: 1
From the console:
rake db:fixtures:load RAILS_ENV=test
rails c test
irb(main):001:0> i = Issue.last
=> #https://api.github.com/repos/bemurphy/issue_triage...", repo_name: nil, user_name: nil, last_touched_at: "2012-11-10 22:20:24", number: 1, created_at: "2012-11-10 23:23:45", updated_at: "2012-11-10 23:23:45", repo_id: 915227508, title: "first test issue in sinatra", html_url: "https://github.com/sinatra/sinatra/issues/1", state: "open", pr_attached: false>
irb(main):002:0> i.repo
Based on the code in your project here, it looks like Issues belong to Repos. I'm wondering if the fixtures are having trouble going "backwards" in that relationship.
You could try using ERB to get the ID of the proper fixture and apply it like so:
repo_id: <%= ActiveRecord::FixtureSet.identify(:issue_triage_sandbox) %>

rspec not saving to database

I'm trying to test the login method below, but it does not seem to be saving the data_provider_user to the database. I've checked using debugger and it works perfectly until it leaves the method and goes back to the rspec code, as shown below. I've also checked to see if the object is valid after it save using .valid? and it is so I'm a bit lost!
def login
require 'TwitterOauth'
#data_provider_user = DataProviderUser.find(params[:id])
if #data_provider_user.twitter?
#request_token = TwitterOauth.request_url
#data_provider_user.access_token = #request_token.token
#data_provider_user.oauth_token_secret = #request_token.secret
if #data_provider_user.save
debugger
redirect_to #request_token.authorize_url
end
end
end
Test:
it "should update the tokens" do
require 'TwitterOauth'
TwitterOauth.stub(:request_url).and_return(#token)
debugger
get :login, {id: #data_provider_user.id}
debugger
#data_provider_user.access_token.should_not eq(nil)
#data_provider_user.oauth_token_secret.should_not eq(nil)
end
Debugger output:
#<DataProviderUser id: 84, user_id: 63, data_provider_id: 63, username: nil, password: nil, created_at: "2013-02-19 15:21:59", updated_at: "2013-02-19 15:21:59", access_token: nil, update_frequency: nil, oauth_token_secret: nil>
#<DataProviderUser id: 84, user_id: 63, data_provider_id: 63, username: nil, password: nil, created_at: "2013-02-19 15:21:59", updated_at: "2013-02-19 15:22:12", access_token: "186553918-sEAEO2fcvtyO1x99eH4Q4XwVcOYatODCQ5f1TwqD", update_frequency: nil, oauth_token_secret: "gLw2PtUyTZfxIan1gJBnbP7icboXbi98KlUoOn7ycVs">
#<DataProviderUser id: 84, user_id: 63, data_provider_id: 63, username: nil, password: nil, created_at: "2013-02-19 15:21:59", updated_at: "2013-02-19 15:21:59", access_token: nil, update_frequency: nil, oauth_token_secret: nil>
Any help would be greatly appreciated!!
------ UPDATE -------
There was actually nothing wrong with the code, the issue was that rspec was not reloading it in time. A way to get around this was to use:
#data_provider_user.reload
before hand which refreshes the object, updating the values accordingly.
There was actually nothing wrong with the code, the issue was that rspec was not reloading it in time. A way to get around this was to use:
#data_provider_user.reload
before hand which refreshes the object, updating the values accordingly.
In spec/rails_helper.rb
Set false to use_transactional_fixtures:
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
Are you sure you're looking at the right database? When you're doing testing it's probably going to myapp_test (myapp being whatever you called your app). You may be looking at your myapp_development database.
If you're accessing the database via rails db chances are you're going into your development database. You can check with select database(); if you're using MySQL. If you want to check your test database, then use RAILS_ENV=test rails db. Again, you can use the select statement to verify you're in the right database.

Mongoid update_attributes not persisting

I'm running an update_attributes command to add two new added fields in one of my user documents in my rails console and it says true on the command being ran, but nothing is persisting in the database.
User.last (simplified):
#<User _id: 4c77d555b1382g539f000022, first_name: "Jason", last_name: "Johnson">
Commands I tried running (following the http://mongoid.org docs):
User.last.update_attributes(nickname: 'Josh', email: 'josh#gmail.com')
User.last.update_attribute(:nickname, 'Josh') # I get a NoMethodError: undefined method `update_attribute'
user.rb
field :first_name
field :last_name
field :nickname # just added this
field :email # just added this
mongoid gem version:
rails (3.0.3)
mongoid (2.0.0.beta.20)
I suspect that you had an empty users collection when you got the NoMethodError.
The following works for Rails 3.2.3, Mongoid 2.4.10.
I suggest copying and running the test so that you have a known environment for each thing that you are testing.
class User
include Mongoid::Document
field :first_name
field :last_name
field :nickname
field :email
end
test/unit/user_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
User.delete_all
end
test "update_attribute" do
User.create(first_name: 'Jason', last_name: 'Johnson')
assert_equal(1, User.count)
p User.last
User.last.update_attribute(:nickname, 'Josh')
p User.last
User.last.update_attributes(nickname: 'Josh', email: 'josh#gmail.com')
p User.last
end
end
test output
Run options: --name=test_update_attribute
# Running tests:
#<User _id: 4fc63ff5e4d30b08ca000001, _type: nil, first_name: "Jason", last_name: "Johnson", nickname: nil, email: nil>
#<User _id: 4fc63ff5e4d30b08ca000001, _type: nil, first_name: "Jason", last_name: "Johnson", nickname: "Josh", email: nil>
#<User _id: 4fc63ff5e4d30b08ca000001, _type: nil, first_name: "Jason", last_name: "Johnson", nickname: "Josh", email: "josh#gmail.com">
.
Finished tests in 0.019296s, 51.8242 tests/s, 51.8242 assertions/s.

Resources