In Rails you can do:
rails c
record = Record.where(name: 'Test Record').first
record.destroy
How can you do the same in Hanami? I've been reading through the docs but I'm struggling to see how to do console commands like Rails to interact with the database objects.
You can do
$ hanami c
UserRepository.new.users.where(name: "Test Record").delete
When a class inherits from Hanami::Repository
delete(id) – Delete the record corresponding to the given id
in Hanami use delete instead of destroy
Related
Recently I have tried using Hanami, Ruby framework. I would like to execute migration with "bulk insert".
I checked following issue discussion.
Proposal: multi_create method for bulk records #406
But, I don't understand how to call ROM object from Hanami. Would you please explain how to do that and any web site to refer ?
Finally I have realized meaning of code.
At first, I wrote bulk_insert as instance method.
somes represents SQL table's name, I could use this with symbol
Repository sample
class SomeRepository < Hanami::Repository
def bulk_insert(data)
command(:create, somes, use: [:timestamps], result: :many).call(data)
end
end
Bulk insert sample
# we can pass array of hash
SomeRepository.new.bulk_insert(some_array)
SomeRepository.new.bulk_insert([{name: "sample1"}, {name: "sample2"}, {name: "sample3"}])
I am writing a Ruby on Rails 4.1 app with Ruby 2.11. The structure is Domain -> Team -> User. So, a user belongs to a team and I sometimes need to make a team from a domain (so they are associated). Think Starbucks -> NY Central Team -> Mr Barista.
I have made some builder classes and put them in app/builders, but when I try to use the class it says uninitialized constant in the Rails console. So, for example, I have the file in app/builders/team_builder.rb:
class TeamBuilder
attr_reader :domain, :params
def initialize(domain, params = {})
#domain = domain
#params = params
end
def build
domain.teams.build(params)
end
end
But when I type TeamBuilder.new(domain, name: 'Team name here!') I get the response
NameError: uninitialized constant TeamBuilder
It seems like it does not recognise the new class I have added above, which makes me think it is not loading it. But I thought that all sub-directories in app/ were loaded.
Totally stumped on this one, and I cannot find a guide or documentation on this (maybe it's there - somewhere...)
Go into your rails console (rails c) and type the following:
YourAppName::Application.config.eager_load_paths
(where YourAppName is whatever it is called in your environment.rb where initialize! is called). This should show you all the paths automatically loaded into your application (before customizing).
It looks like the automatic path adding magic is happening in here https://github.com/rails/rails/blob/master/railties/lib/rails/engine/configuration.rb
The docs indicate you can run:
bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths'
to view in terminal
I'm using Datamapper+SqLite. I need to use a direct query like so:
adapter = DataMapper.repository(:default).adapter
adapter.execute("SELECT * FROM stuff")
How do I see the output of this thing? I see type DataObjects::Sqlite3::Result in irb? Also, any recommendations on how to see SQLite results, ala PHPMyAdmin for Mac?
You want to use adapter.select. execute is meant for operations that do not return results.
Example:
> adapter.select('select * from posts')
=> [#<struct id=1, title="T1", body="B1">,
#<struct id=2, title="T2", body="B2">]
You can see the documentation on the different adapter methods here:
http://rdoc.info/github/datamapper/dm-do-adapter/DataMapper/Adapters/DataObjectsAdapter
But if all need is just a pure SQLite adapter then you should be looking at something like the sqlite3 gem instead: http://rubygems.org/gems/sqlite3
I recently upgraded an old Rails 2.3 app (Ruby 1.8.7) to Rails 3.2 / Ruby 1.9.3. When using ActiveRecord serialize and trying to access a serialized attribute, i get:
ActiveRecord::AttributeMethods::Serialization::Attribute returned.
.unserialize then returns the actual value.
The real strange thing is, that there a some models which have a normal behavior.
Can someone help me?
I now figured out when this problem happens, but i still dont know why:
This works:
User < ActiveRecord::Base
serialize :options, Hash
end
Options return {}
If i call the model itself before serialize, like that:
User < ActiveRecord::Base
USER_LIST = User.all.map {|u| [u.name, u.id]}
serialize :options, Hash
end
i get ActiveRecord::AttributeMethods::Serialization::Attribute
Anybody got an idea?
Try using unserialized_value rather than unserialize, so it doesn't matter whether or not it has already been unserialized. For reference, view the source for the unserialized_value method here http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/Attribute.html
I use
rails generate model mynewmodel string:name string:description
to generate a new model. How do I deploy this new model to my develop databse ? I already have a bunch of databases in my sqlite db.
I have tried
rake db:migrate
it seemed having trouble to generate this new table in db.
update: added error message
== CreateMynewmodels: migrating ===============================================
-- create_table(:mynewmodels)
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `name' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x3ad5c50>
Tasks: TOP => db:migrate
Thanks
The order of your fieldname:type combo is incorrect. Try
rails generate model mynewmodel name:string description:string
The error in rails generate model mynewmodel string:name string:description
You should swap string and name
rails generate model mynewmodel name:string description:string
Use name:string instead of string:name same for description
Great article for advanced usage:
Advanced Rails model generators
Pay attention that you have to wrap parameter price:decimal{10,2} to
quotes. It's vital and you may have incorrect behavior of generator if
you don't do it.