Iam working in ruby on rails for fetching the existing db tables from remote server mechine(SQL SERVER) .Actually i don't know how to do this.Am following this way.please rectify me
My problem is while tying to run uninitialized constant TrDeviceDetailsController::TRDeviceDetail is getting.
I set the following in database.yml file.
development:
adapter: sqlserver
mode: odbc
database: BObd
dsn: newdb_64
username: ush
password: Ushu
host: ws1a20\SQLEXPRESS
The table exist in the db BOdb is TRDeviceDetails.I created models and controller using the command
rails generate model `TRDeviceDetail`
rails generate controller `TRDeviceDetails`
And in controller i put the following
class TrDeviceDetailsController < ApplicationController
def show
#devices = TRDeviceDetail.find(:all)
end
end
model file
class TrDeviceDetail < ActiveRecord::Base
# attr_accessible :title, :body
attr_accessible :UniqueDeviceID
end
where UniqueDeviceID is the existing column in table TrDeviceDetails
and created a show.html.erb file for displaying the UniqueDeviceID
<h1>TrDeviceDetails#show</h1>
<p>Find me in app/views/tr_device_details/show.html.erb</p>
<%#device.inspect%>
what i need is,get existing tables from remote machine.How it is possible and why this error is occurring?
you should use
#devices = TrDeviceDetail.find(:all) # small 'r'
as the generated class is
class TrDeviceDetail < ActiveRecord::Base
UPDATE:
If the table name is not something that follows convention, you should set the table_name explicitly
class TrDeviceDetail < ActiveRecord::Base
set_table_name 'TRDeviceDetails'
end
Related
I have a specific database (called account_base) for my account :
class Account < ActiveRecord::Base
establish_connection :account_base
...
end
I have user from app rails database :
class User < ActiveRecord::Base
belongs_to :account
end
When I try User.first.account this work fine.
Now I want get all users with account => state == Account::STATE_ACTIVED, I try :
User.includes(:account).where('accounts.state' => Account::STATE_ACTIVED)
but I have this error :
Mysql2::Error: Table 'RAILS_DATABASE_NAME.accounts' doesn't exist
I try with where('account_base.accounts.state' to specify the bd, but not works
Any idea ?
I am implementing Rails Admin for a client and I have two models Job and Candidacy. A Job has many candidacies.From Rails Admin Job Panel, I would like to do a date filter to search between two dates if there is any candidacies.
Here is the code :
# app/models/job.rb
class Job < ApplicationRecord
has_many :candidacies
end
# app/models/candidacy.rb
class Candidacy < ApplicationRecord
belongs_to :job
end
# config/initializers/rails_admin.rb
RailsAdmin.config do |config|
config.model 'Job' do
list do
field :candidacies, :date do
searchable [{Candidacy => :created_at}]
end
end
end
end
When I click on Candidacies Filter and filter between two dates.
It raises an error
ActiveRecord::StatementInvalid in RailsAdmin::Main#index
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "candidacies"
I have all the detail about the SQL query and it is not loading the candidacies table.
SELECT "jobs".* FROM "jobs" WHERE candidacies.created_at BETWEEN '2019-12-11 00:00:00' AND '2019-12-14 23:59:59'
Any idea how to config rails admin to load the table candidacies ?
Thanks !!!
1st Edit :
It works when a add a default_scope to my model Job.
But I don't know if it is the proper way to do it.
# app/models/job.rb
class Job < ApplicationRecord
has_many :candidacies
default_scope { eager_load(:candidacies) }
end
To avoid eager loading the other table in all future Job queries i suggest you keep the scope only on the job list config.
# config/initializers/rails_admin.rb
RailsAdmin.config do |config|
config.model 'Job' do
list do
field :candidacies, :date do
searchable [{Candidacy => :created_at}]
end
scopes [:with_candidacies]
end
end
end
And the model
# app/models/job.rb
class Job < ApplicationRecord
has_many :candidacies
scope :with_candidacies, -> { eager_load(:candidacies) }
end
Rails admin will auto apply the first scope.
I have migrated from Paperclip to Carrierwave, and using Carrierwave_direct to upload images directly to S3.
class User < ActiveRecord::Base
mount_uploader :profile_picture, ProfilePictureUploader, :mount_on => :profile_picture_file_name
So, in my schema, I don't have column profile_picture but profile_picture_file_name in my users table.
This creates problem when I am trying to create the #uploader instance variable
class ProfilePictureController < ApplicationController
def show
#user=current_user
#uploader = #user.profile_picture_file_name
#uploader.success_action_redirect = crop_url
end
This throws an error, when a user is trying to upload the Profile Image,
undefined method `success_action_redirect=' for nil:NilClass
I guess this could fix it:
#uploader = #user.profile_picture
Using Rails 3, I've changed the name of a table in the model like this:
# app/models/product.rb
class Product < ActiveRecord::Base
set_table_name "items"
end
But when I try setting up tests, I get the following error:
Started
E
Finished in 0.027396 seconds.
1) Error:
test_the_truth(CustomerTest):
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'project2_test.products' doesn't exist: DELETE FROM `products`
1 tests, 0 assertions, 0 failures, 1 errors
Any idea how I can let it know about Products?
OK found the answer here:
http://www.missiondata.com/blog/systems-integration/80/rails-fixtures-with-models-using-set_table_name/
Had to change the name of the Fixture yml file from Products to Items.
Rather than alter the class directly, you should create a migration. This will allow Rails to smoothly change the database, and allow any others working on the project to change their database in the same manner.
Write a change method which uses rename_table.
class RenameProductsToItems < ActiveRecord::Migration
def change
rename_table :items, :products :string
end
end
So, I created the following migration:
class AddAclToUsers < ActiveRecord::Migration
def self.up
add_column :users, :acl, :integer
end
def self.down
remove_column :users, :acl
end
end
however, after modifying the various erb files in the view, values entered in edit.html.erb are not saved to the database.
I can manually start SQlite3 and select * the table and see that the column was created but no values are entered. I can also manually UPDATE or INSERT numbers into the new column which the controller queries and displays correctly.
Any suggestions on what may be wrong with the frameworks udate/save??
-daniel
Please check if there is a list of attr_accessible in your User model. If you are using Devise like gem/plugin for authentication you would have a list of attr_accessible in the model.
Add new attribute (acl in your case) to the attr_accessible list.
If this is not the case, I would like you to paste your view, controller and model code