Incorrect association in fixtures - ruby-on-rails

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

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

Why can't I save two or more siblings with mongoid-tree?

I'm using this engine: https://github.com/opennorth/popolo-engine
As you can see in the model, organization includes Mongoid::Tree.
For whatever reason, I cannot seem to create/save more than one record on a level.
I can have two organisations, one a parent and one a child, but I can't then add a second child. I also cannot have two 'top level' nodes that sit without parents.
Popolo::Organization.create(name:"Org1")
=> #<Popolo::Organization _id: 5405dd09360e2e5421000002, created_at: 2014-09-02 15:06:49 UTC, updated_at: 2014-09-02 15:06:49 UTC, parent_id: nil, parent_ids: [], name: "Org1", classification: nil, founding_date: nil, dissolution_date: nil, image: nil>
Popolo::Organization.create(name:"Org2")
=> #<Popolo::Organization _id: 5405dd0c360e2e5421000003, created_at: 2014-09-02 15:06:52 UTC, updated_at: 2014-09-02 15:06:52 UTC, parent_id: nil, parent_ids: [], name: "Org2", classification: nil, founding_date: nil, dissolution_date: nil, image: nil>
Popolo::Organization.all.size
=> 1
EDIT: This works okay in the test environment.
I restarted my mongodb service and it worked!
sudo rm /var/lib/mongodb/mongod.lock
service mongodb start
Very odd indeed.

Why does ActiveRecord association return two copies of every associated object

I have...
class Report < ActiveRecord::Base
has_and_belongs_to_many :elements
end
class Element < ActiveRecord::Base
has_and_belongs_to_many :reports
end
I'm seeing some very weird behaviour at the command line:
$Element.all
[#<Element id: 1, name: "fdafda", created_at: "2013-03-12 02:10:56", updated_at: "2013-03-12 02:10:56">,
#<Element id: 2, name: "Foo", created_at: "2013-03-14 10:46:56", updated_at: "2013-03-14 10:46:56">,
#<Element id: 3, name: "Bar", created_at: "2013-03-14 10:47:03", updated_at: "2013-03-14 10:47:03">]
$ Report.first.elements
[#<Element id: 2, name: "Foo", created_at: "2013-03-14 10:46:56", updated_at: "2013-03-14 10:46:56">,
#<Element id: 2, name: "Foo", created_at: "2013-03-14 10:46:56", updated_at: "2013-03-14 10:46:56">,
#<Element id: 3, name: "Bar", created_at: "2013-03-14 10:47:03", updated_at: "2013-03-14 10:47:03">,
#<Element id: 3, name: "Bar", created_at: "2013-03-14 10:47:03", updated_at: "2013-03-14 10:47:03">]
(rdb:2) Report.first.elements.uniq
[#<Element id: 2, name: "Foo", created_at: "2013-03-14 10:46:56", updated_at: "2013-03-14 10:46:56">,
#<Element id: 3, name: "Bar", created_at: "2013-03-14 10:47:03", updated_at: "2013-03-14 10:47:03">]
How is the duplication of elements in Report.first.elements even possible? And how can I stop it?
rails -v
Rails 3.2.11
$ ruby -v
ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin12.2.1]
Found the answer. Had a report selector in the form at reports/id/elements/new. If the user selected the current report in the report selector, two links between the element and the report would be created in the join table.

Rails Syntax for entering data in sqlite

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

Resources