I have a Model representing a Pricing Table with lots of entries and i want to offer the possibility to create a new Pricing with values from an existing entry.
Does anyone know how to do this, then Sequel is in use?
I tried dup and clone but in both cases the id is still there from the existing Model and thus will update the existing entry.
If i try to set the id by hand i get following error:
Sequel::InvalidValue: nil/NULL is not allowed for the id column
So i need a to find a Way to create a Model which is new but has prefilled values without having them to set in the code by hand.
Any ideas?
found it:
new_pricing = Pricing.new(oldprice.attributes.tap{|attr| attr.delete("id")})
i get the attributes from the old model as hash, then remove the id and create a new Model by passing the attributes except the id.
The model.attributes solution didn't work for me. Sequel models have to_hash which is roughly equivalent, but to_hash doesn't return deserialized values. If you are using serializers (for jsonb fields, etc), simply passing a to_hash to new will fail because the values are not yet deserialized.
Here's the solution that works for me:
user = User.find(id: 123)
# freeze to avoid accidentally modifying the original user
user.freeze
# duplicate the record, deserialize values, and delete the primary key
# deserialization is useful if your model is using jsonb fields
record_copy = user.to_hash.merge(user.deserialized_values)
record_copy.delete(:id)
duplicate_user = User.new
# pass the has via `set_all` to avoid initialization callbacks
duplicate_user.set_all(record_copy)
# ... other important callbacks
duplicate_user.save
Related
I have a model, Course, which has_many tees. The tees are created individuall from a hash derived from an api e.g
course_h[:tees].each do |tee_h|
course.tees.create!(tee_h)
end
Later I retrieve another hash and want to compare the new hash with the saved tees. I would like to have the same order as the original hash, and assumed that I could do this with course.tees.order(created_at: :asc). This works in the test environment, but in development environment, the order is not the same. Why is this, and is there a way to fix it?
It is tempting to use the object id for this purpose but as noted in the comments, the ordering of the object id or the created_at field cannot be relied upon. It is safer to add an ordering field, say :num. The code would then be
course_h[:tees].each_with_index do |tee_h, i|
Tee.create!(tee_h.merge(num: i)
end
and then use the ordering course.tees.order(num: asc)
Is there a way to restrict what hstore columns can be saved? I've got below code for doing this so far:
store_accessor :widget_locations, :left_area1, :mid_area1, :left_area2, :mid_area2, :right_area2
but this seems to still allow other key names to be saved ie. middle_area123
Also how am I able to update hstore like update_attributes or update?
I could be wrong but my guess is that you are making calls on widget_locations like
item.widget_locations[:left_area1] = thing
If so, you should change that to
item.left_area1 = thing
because you told the store_accessor to create attributes :left_area1, :mid_area1, :left_area2, :mid_area2, :right_area2 that will be serialized to database column :widget_locations. Now these attributes will behave like normal attributes, so you can put validations on them etc.
This also allows you to update an item as usual:
item.update(name: 'Test', left_area: 'garden', mid_area: 'livingroom')
The catch with a hstore is that accessing the serialized column will allow you to add new unknown attributes, so it is best to directly access the attributes you explicitly specified.
I am trying to create an empty model, populate it and then add it to the database.
I am looking through Google for the syntax for instantiating a simple model that I can set fields in, but there does not seem to be much documentation for that.
Is that just not the intended useage pattern? If it is, how can I just create the empty model?
Thanks!
An ActiveRecord model works based on what fields it's related table has in your db. If you have no db yet you have no fields. The usage pattern goes like this:
$ rails g model client name:string
#stuff happens
$ rake db:migrate
You now have a model associated with a clients table that has a string attribute called name.
Now in your controller you can use this by
#client = Client.new
#client.name = "foo"
#client.save
Which will create the model object, set the name, and persist it to the db
You should read up on the Rails Guides. Your current issue is covered at this link, but you really need to read up on getting started.
Is there any gem/plugin for ruby on rails which gives the ability to define custom fields in a model at runtime with no need to change the model itself for every different field.
I'm looking for something like Redmine acts_as_customizable plugin which is packaged as a gem usable in the rails way, i.e.
gem 'gemname'
rails g something
rails db:migrate
class Model < ActiveRecord::Base
acts_as_something
end
Here are the CustomField and the CustomValue classes used in Redmine.
Edit:
Since my question is not clear I add a brief use case which explains my need better:
I want users to be able to design their own forms, and collect data
submitted on those forms. An important decision is the design of how
these custom dynamic records are stored and accessed.
Taken from here, in this article approach the problem with different ideas, but they all have drawbacks. For this reason I'm asking if the issue has been approached in some gem with no need to rethink the whole problem.
I'm not aware of a gem that does this, but serialize works quite well and it's a built-in. You get a NoSQL-ish document store backed by JSON/YAML.
If you allow user to create a custom form, you can pass nested arrays et cetera directly into the attribute. However, if you need to validate the structure, you're on your own.
I'm afraid it could be tricky and complicated to do it in ActiveRecoand (generally in standard relational database). Take a look at http://mongoid.org/docs/documents/dynamic.html - this mechanism is using nosql feature.
You can also may try the following trick:
1/ Serialize a hash with your custom fields in the database column, for example { :foo => 'bar', :fiz => 'biz' }
2/ After load a record from database do some metaprogramming and define corresponding methods on the record's singleton class, for instance (assume that custom fields are stored and serialized in custom_fields column):
after_initialize :define_custom_methods
# ..or other the most convinient callback
def define_custom_methods
# this trick will open record's singleton class
singleton_class = (class << self; self; end)
# iterate through custom values and define dynamic methods
custom_fields.each_with_key do |key, value|
singleton_class.send(:define_method, key) do
value
end
end
end
Since rails 3.2 you can use store method. Just include following in your model:
store :properties, accessors: [:property1, :property2, :property3...]
You only need to change your model once (to add properties field to db table). You can add more properties later without altering the schema.
The way this works is by serializing properties hash into YAML and saving it into database. It it suitable for most cases, but not if you'd like to use these values in db queries later.
I don't know a gem, but this can be accomplished be creating a table called custom_fields with a name column and possibly a datatype column if you wanted to restrict fields by datatype.
Then you create a join table for a custom field to your desired table and a value and do whatever validations you want.
I am using Ruby on Rails 3 and I followed the Tableless models in Rails istructions in order to apply that to a my model Account.
All works, but if I do
#test = account.id
a debug of #test results in a nil value and seems not accessible at all.
In a comment of this question #Wukerplank said:
You are right. I suppose id has a special status in ActiveRecord. I think it would only be set after the record is persisted in some database.
How can I retrive\access the id attribute value?
UPDATED
Trying and re-trying I discovered that a possible solution is to make all attributes 'attr_accessible' (if I make just the 'id', I get all other value 'nil'), but I think it is a very dangerous solution.
Another solution is to create a new class attribute that acts as the id, but why I have to do that if I have already the id?!
Why don't you use ActiveModel instead? Check this screencast.