Odoo 10 related field - field

I created custom field to res.company. But in my other class, i can not call this field,
this field inherited to res.company,
company_sicilno = fields.Char(string="Sicil No :")
and this field is in my other class,
sicil_no = fields.Char(related='res.company.company_sicilno',store=True)
i could not find mistake, it does not show any error message.
How can i call this res.company.company_sicilno in my other field?

You should have field company_id:
company_id = fields.Many2one(
'res.company',
string='Company',
default=lambda self: self.env.user.company_id )
And you can have your related field:
sicil_no = fields.Char(related='company_id.company_sicilno',store=True)

You should have a Many2one field to achieve this. First you declare a Many2one field for res.company
company_id = fields.Many2one('res.company',string="Company")
Now you can have your related field like the following
sicil_no = fields.Char(related='company_id.company_sicilno',store=True)
sicil_no will get value form company_id field

I solved the problem
company_id = fields.Many2one('res.company',string="Company",default=lambda self: self.env['res.company'].search([]))
Thank you for your help..

Related

Apply where condition if it exists in rails

is there a way in rails activerecord to apply the where condition based on a condition ?
For example:
Say I want to apply the filter if it is present in the input parameters.
Assume #name is a mandatory field and #salary is an optional field.
#name = "test_name"
#salary = 2000
I want to do something like below:
Employee.where(name: #name)
.where(salary: #salary) if #salary.present?
I know to make it in multiple db calls, but I'm looking for an option to make this happen in a single db call.
You can assign the result of the 1st where to a variable and invoke the 2nd where conditionally:
#employees = Employee.where(name: #name)
#employees = #employees.where(salary: #salary) if #salary.present?
You can query only by parameters that are present:
args = { name: #name, salary: #salary.presence }
Employee.where(args.compact)
You can just add all possible arguments into one hash and remove the ones that are nil with Hash#compact:
Employee.where({ name: #name, salary: #salary }.compact)

How can I add information to custom user field (Devise)

I create a custom field in user (using a Devise) in my Rails project, varible called information. Type is string.
Somethigh like that: current_user.information
How can I add another string to end of this string?
I need a method then do like this:
current_user.information << some_varible
And save changes.
If the value of current_user.information is a string you can append to it like you stated.
s = current_user.information
s << "New string addition"
s.save
Except the << method mentioned on #Rockwell's answer, you can achieve your goal by using String#concat as well:
s = current_user.information
s.concat("New string addition")
s.save

rails use form input value before save

I have a form in rails with input values, and 1 of the values is a LOV (List of Values) and a second input field is also a LOV but depends on the input of the other field.
The input value of the first field is not saved in between. So I need to use that field value before it is saved.
So, the example:
I choose a company from the list of values of all companies for the field supplier, the second field supplier_address will be a LOV with all the addresses of that company, so the LOV of the second field is dependent on the value chosen in the first field, company.
What I tried:
def new
#purchase_requisition = PurchaseRequisition.new
#purchase_requisition.number = find_next_user_value("Purchase_Requisition")
##purchase_requisition.supplier_address_id = PurchaseRequisition.new.purchase_requisition_params[:supplier_id]
#purchase_requisition = PurchaseRequisition.new(purchase_requisition_params)
respond_to do |format|
#purchase_requisition.supplier_address_id = PurchaseRequisition.new.purchase_requisition_params[:supplier_id]
end
end
but I still get the error:
param is missing or the value is empty: purchase_requisition
Can someone please help me?
Thank you!!!
The error you're encountering isn't being caused by the code you've provided. You're probably using strong parameters and have a method like this:
def purchase_requisition_params
params.require(:purchase_requisition).permit(# some list of attributes #)
end
The problem is that params[:purchase_requisition] doesn't exist. Probably because the form_for in your view isn't referencing a purchase_requisition object. Try adding as: to your form_for to send your params under that param key:
form_for #requisition, as: :purchase_requisition, ....
Otherwise, you'll have to post more details about your view and controller to help isolate the issue you're having.
Also, in your controller code you want:
PurchaseRequisition.new(purchase_requisition_params[:supplier_id])
Instead of:
PurchaseRequisition.new.purchase_requisition_params[:supplier_id]
Supposing, all of your parameters belong to the same object (there isn't any nested attribute), this can be what you are looking for:
def purchase_requisition_params
params.require(:purchase_requisition).permit(:org_id, :document_type, :number, :supplier_id, :supplier_address_id, :partner_id, :project_id, :construction_site_id, :purchase_order_date, :expected_receiving_date, :promised_receiving_date, :supplier_order_number, :supplier_shipping_number, :supplier_invoice_number, :currency, :status) ##you don't need id here
end

Rails - find by with two fields using OR instead of AND?

I am familiar with doing a find on two fields:
user = User.find_by_username_and_email(params[:username], params[:email])
However, I would like to retrieve one record where the username OR email equals the same field. Something like:
user = User.find_by_username_or_email(params[:text])
Which I know doesn't work. Is this possible? Any help is a appreciated.
Just to expand on the answer by #xdazz, you can use the following syntax to allow searching any number of fields by the same value without having to repeat the value:
user = User.where('username = :text or email = :text', :text => params[:text]).first
Very useful when you come to search postal address fields, for example.
2.2.1 Placeholder Conditions
You could use .where :
user = User.where('username = ? or email = ?', params[:text], params[:text]).first

get model attribute dynamically in rails 3

How can I get the attribute from the model object dynamically? I have the attribute of the User object as string:
u = User.find(1)
Can I do something like u.get("user_id")
You could try using the ActiveRecord model instance like a hash.
u = User.find(1)
name = u[:name]
field = "first_name"
first_name = u[field]
Yet another approach:
attr = :first_name
#user.read_attribute attr
Try this
user = User.find(1)
then something like this should do what you require
user.send('field_name')
Try this
u = User.find(1)
attr = "first_name"
u.send(attr)
Not sure if I totally understand your questions. Try something like:
User.find(1).name
if you mean you only want to fetch from DB specific attribute you can do:
User.find(1,:select => :name)

Resources